PostgreSQL_cheatsheet
PostgreSQL_cheatsheet
The PostgreSQL cheat sheet provides you with the common PostgreSQL commands and statements.
# Getting Started
- Getting started psql commands
postgres=# \l
-U -h -p -d psql -U admin -h 192.168.1.5 -p 2506 -d mydb Connect remote PostgreSQL
postgres=# \c postgres -c psql -c '\c postgres' -c '\dt' Execute a SQL query or command
Getting help
# PostgreSQL Working
- Recon - Databases - Tables
\dt *.*.
SHOW ALL; SELECT current_database();
Become the postgres user, if you have permission errors Add column Select all data
UPDATE <table_name>
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA Insert into a table, with an auto-incrementing primary key SET <column_1> = <value_1>, <column_2> =
public TO <user_name>; <value_2>
INSERT INTO <table_name> WHERE <column_1> = <value>;
Grant permissions to select, update, insert, delete, on a all VALUES (DEFAULT, <value1>);
tables Delete all data
Create user SELECT schema_name FROM Calculate age between two dates
information_schema.schemata;
# PostgreSQL Commands
Tables Query buffer Informational
\d <table> Describe table \e [FILE] Edit the query buffer (or file) \l[+] List all databases
\d+ <table> Describe table with details \ef [FUNC] Edit function definition \dn[S+] List schemas
\dt List tables from current schema \p Show the contents \di[S+] List indexes
\dt *.* List tables from all schemas \r Reset (clear) the query buffer \du[+] List roles
\dt <schema>.* List tables for a schema \s [FILE] Display history or save it to file \ds[S+] List sequences
\dp List table access privileges \w FILE Write query buffer to file \df[antw][S+] List functions
Connection Formatting
\dl List large objects
\c [DBNAME] Connect to new database \a Toggle between unaligned and aligned
\dT[S+] List data types
\encoding [ENCODING] Show or set client encoding \C [STRING] Set table title, or unset if none
\da[S] List aggregates
\password [USER] Change the password \f [STRING] Show or set field separator for unaligned
\db[+] List tablespaces
\conninfo Display information \H Toggle HTML output mode
\dc[S+] List conversions
\t [on|off] Show only rows
\dC[+] List casts
Set or unset HTML <table> tag
\T [STRING]
attributes \ddp List default privileges
Input/Output Variables
\des[+] List foreign servers
Import/export table \prompt [TEXT] NAME Set variable
\copy ... \dew[+] List foreign-data wrappers
See also: copy
Set variable (or list all if no
\set [NAME [VALUE]]
\echo [STRING] Print string parameters) \dF[+] List text search configurations
\i FILE Execute file \unset NAME Delete variable \dFd[+] List text search dictionaries
\o [FILE] Export all results to file \dFp[+] List text search parsers
\qecho [STRING] String to output stream \dFt[+] List text search templates
# Miscellaneous
- Backup - Restore - Remote access
Use pg_dumpall to backup all databases Restore a database with psql Get location of postgresql.conf
$ pg_dumpall -U postgres > all.sql $ psql -U user mydb < mydb_backup.sql $ psql -U postgres -c 'SHOW config_file'
Use pg_dump to backup a database Restore a database with pg_restore Append to postgresql.conf
-a Dump only the data, not the schema -U Specify a database user Append to pg_hba.conf (Same location as postgresql.conf)
-C Create database before restoring -e Exit if an error has encountered Restart PostgreSQL server
-t Dump the named table(s) only Format (c: custom, d: directory, t: tar, p: plain text
-F $ sudo systemctl restart postgresql
sql(default))
-F Format (c: custom, d: directory, t: tar)
Use pg_dump -? to get the full list of options Use pg_restore -? to get the full list of options
- Import/Export CSV
# Also see