PostgreSQL Connection Commands
Command |
Description |
psql -U postgres |
Connect to PostgreSQL as the postgres superuser. |
psql -U <user> -d <database> |
Connect to PostgreSQL as a specific user to a specific database. |
psql -U <user> -d <database> -h <host> |
Connect to PostgreSQL on a specific host, user, and database. |
PostgreSQL CLI (psql) Commands
Command |
Description |
\c <database> |
Switch to a different database. |
\password <user> |
Change the password for a specified user. |
\l |
List all available databases on the server. |
\dt |
List all tables in the current database. |
\du |
List all users (roles) on the server. |
\df |
List all functions in the current database. |
\dv |
List all views in the current database. |
\dn |
List all schemas in the current database. |
\dp |
List all access permissions for tables and views. |
\di |
List all indexes in the current database. |
\ds |
List all sequences in the current database. |
\d+ <table_name> |
Show detailed information about a specific table, including columns, types, and constraints. |
\q |
Quit the psql interactive terminal. |
\x |
Toggle expanded display mode for query results. |
PostgreSQL Backup and Restore Commands
Command |
Description |
pg_dump <database> > backup.sql |
Backup an entire PostgreSQL database to a SQL file. |
psql <database> < backup.sql |
Restore a PostgreSQL database from a SQL file. |
This cheat sheet provides essential PostgreSQL commands for developers and database administrators. It covers connecting to your PostgreSQL server, managing databases and their objects using the psql
command-line interface, and performing crucial backup and restore operations. Mastering these commands will significantly enhance your efficiency when working with PostgreSQL.
For more in-depth information, refer to the official PostgreSQL Documentation.