Difference between revisions of "PostgreSQL Cheat Sheet"
Jump to navigation
Jump to search
Line 40: | Line 40: | ||
postgres=# DROP DATABASE IF EXISTS <database_name>; | postgres=# DROP DATABASE IF EXISTS <database_name>; | ||
+ | |||
+ | '''Export PostgreSQL Database to Dump File''' | ||
+ | |||
+ | [root@gejoreuy ~]# pg_dump -U <username> -h <hostname> -p <port> <source_database_name> -f <dump_file_name> | ||
+ | |||
+ | '''Import PostgreSQL Database Dump File''' | ||
+ | |||
+ | [root@gejoreuy ~]# psql -U <username> -h <hostname> -p <port> -d <destination_database_name> -f <dump_file_name> |
Revision as of 16:45, 14 February 2021
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.
Create New Database
postgres=# CREATE DATABASE <database_name>;
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>;
Export PostgreSQL Database to Dump File
[root@gejoreuy ~]# pg_dump -U <username> -h <hostname> -p <port> <source_database_name> -f <dump_file_name>
Import PostgreSQL Database Dump File
[root@gejoreuy ~]# psql -U <username> -h <hostname> -p <port> -d <destination_database_name> -f <dump_file_name>