Difference between revisions of "PostgreSQL Cheat Sheet"
Jump to navigation
Jump to search
Line 17: | Line 17: | ||
'''Find PostgreSQL Active Connection''' | '''Find PostgreSQL Active Connection''' | ||
− | postgres=# SELECT * FROM pg_stat_activity WHERE datname = ' | + | postgres=# SELECT * FROM pg_stat_activity WHERE datname = '<database_name>'; |
'''Terminate All Active Connections in Database''' | '''Terminate All Active Connections in Database''' |
Revision as of 15:28, 15 December 2020
List PostgreSQL Databases
postgres=# \l
List PostgreSQL Schemas
postgres=# \dn
List PostgreSQL Tables
postgres=# \dt
List PostgreSQL Users
postgres=# \du
Find PostgreSQL Active Connection
postgres=# SELECT * FROM pg_stat_activity WHERE datname = '<database_name>';
Terminate All Active Connections in Database
postgres=# SELECT pg_terminate_backend (pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '<database_name>';
Check If The Server is Postgres Master or Slave
postgres=# SELECT pg_is_in_recovery();
If it's true, we're on a slave; if false, we're in master.
Delete or Drop User from Database
postgres=# DROP USER IF EXISTS <user_name_1>, <user_name_2>, ..., <user_name_n>;
Delete or Drop Database
postgres=# DROP DATABASE IF EXISTS <database_name>;