- Posgres-cheatsheet (gist.github.com)
PostgreSQL Cheat Sheet
The PostgreSQL cheat sheet provides you with the common PostgreSQL commands and statements.
Also see
Miscellaneous
Import/Export CSV
Export table into CSV file
\copy table TO '<path>' CSV \copy table(col1,col1) TO '<path>' CSV \copy (SELECT...) TO '<path>' CSV
Import CSV file into table
\copy table FROM '<path>' CSV \copy table(col1,col1) FROM '<path>' CSV
See also: Copy
Remote access
Get location of postgresql.conf
$ psql -U postgres -c 'SHOW config\_file'
Append to postgresql.conf
listen_addresses = '*'
Append to pg_hba.conf (Same location as postgresql.conf)
host all all 0.0.0.0/0 md5 host all all ::/0 md5
Restart PostgreSQL server
$ sudo systemctl restart postgresql
Restore
Restore a database with psql
$ psql -U user mydb < mydb\_backup.sql
Restore a database with pg_restore
$ pg\_restore -d mydb mydb\_backup.sql -c
- Â
-U
 Specify a database user - Â
-c
 Drop database before recreating - Â
-C
 Create database before restoring - Â
-e
 Exit if an error has encountered - Â
-F
 Format (c
: custom,d
: directory,t
: tar,p
: plain text sql(default)) Usepg_restore -?
to get the full list of options
Backup
Use pg_dumpall to backup all databases
$ pg\_dumpall -U postgres > all.sql
Use pg_dump to backup a database
$ pg\_dump -d mydb -f mydb\_backup.sql
- Â
-a
 Dump only the data, not the schema - Â
-s
 Dump only the schema, no data - Â
-c
 Drop database before recreating - Â
-C
 Create database before restoring - Â
-t
 Dump the named table(s) only - Â
-F
 Format (c
: custom,d
: directory,t
: tar) Usepg_dump -?
to get the full list of options
PostgreSQL Commands
Large Objects
\lo_export LOBOID FILE
\lo_import FILE [COMMENT]
\lo_list
\lo_unlink LOBOID
Misc
- | - |
---|---|
\cd [DIR] | Change the directory |
\timing [on|off] | Toggle timing |
\! [COMMAND] | Execute in shell |
\! ls -l | List all in shell |
Variables
- | - |
---|---|
\prompt [TEXT] NAME | Set variable |
\set [NAME [VALUE]] | Set variable (or list all if no parameters) |
\unset NAME | Delete variable |
Input/Output
- | - |
---|---|
\copy ... | Import/export table See also: copy |
\echo [STRING] | Print string |
\i FILE | Execute file |
\o [FILE] | Export all results to file |
\qecho [STRING] | String to output stream |
Formatting
- | - | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
\a | Toggle between unaligned and aligned | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\C [STRING] | Set table title, or unset if none | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\f [STRING] | Show or set field separator for unaligned | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\H | Toggle HTML output mode | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\t [on|off] | Show only rows | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
\T [STRING] | Set or unset HTML
Connection
Informational
Query buffer
PostgreSQL WorkingDatesShow current date YYYY-MM-DD Calculate age between two dates Show current time with time zone Make dates using integers
SchemaList schemas
DataSelect all data Read one row of data Search for data Insert data Update data Delete all data Delete specific data
Columns Update column Delete column Update column to be an auto-incrementing primary key Insert into a table, with an auto-incrementing primary key
PermissionsBecome the postgres user, if you have permission errors Grant all permissions on database Grant connection permissions on database Grant permissions on schema Grant permissions to functions Grant permissions to select, update, insert, delete, on a all tables Grant permissions, on a table Grant permissions, to select, on a table
TablesList tables, in current db List tables, globally List table schema Create table, with an auto-incrementing primary key
DatabasesList databases Connect to database Show current database
ReconShow version Show system status Show environmental variables List users Show current user Show current user's permissions Show current database Show all tables in database List functions
Getting StartedSwitch and connect List all databases Connect to the database named postgres Disconnect
Getting help
psql commands
|