PostgreSQL Cheat Sheet & Quick Reference
PostgreSQL Cheat Sheet & Quick Reference
3k Follow Me
PostgreSQL cheatsheet
The PostgreSQL cheat sheet provides you with the common PostgreSQL commands
and statements.
# Getting Started
Getting started psql commands
postgres=# \l
psql -U admin -h 192.168.1.5 -p 2506 -d
-U -h -p -d Connect remote PostgreSQL
mydb
Connect to the database named postgres
-W psql -W mydb Force password
postgres=# \c postgres
Execute a SQL query or
-c psql -c '\c postgres' -c '\dt'
command
Disconnect
-H psql -c "\l+" -H postgres > database.html Generate HTML report
postgres=# \q
postgres=# \! -l psql -l List all databases
Getting help
# PostgreSQL Working
Recon Databases Tables
SELECT rolname FROM pg_roles; CREATE DATABASE <database_name> WITH List table schema
OWNER <username>;
Show current user \d <table_name>
Drop database \d+ <table_name>
SELECT current_user;
SELECT column_name, data_type,
DROP DATABASE IF EXISTS
character_maximum_length
Show current user's permissions <database_name>;
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '<table_name>';
\du Rename database
Create table
Show current database ALTER DATABASE <old_name> RENAME TO
<new_name>;
CREATE TABLE <table_name>(
SELECT current_database(); <column_name> <column_type>,
<column_name> <column_type>
);
Show all tables in database
List functions
CREATE TABLE <table_name> (
<column_name> SERIAL PRIMARY KEY
\df <schema>
);
Delete table
Become the postgres user, if you have Add column Select all data
permission errors
ALTER TABLE <table_name> IF EXISTS SELECT * FROM <table_name>;
sudo su - postgres ADD <column_name> <data_type>
psql [<constraints>]; Read one row of data
Grant all permissions on database Update column SELECT * FROM <table_name> LIMIT 1;
GRANT ALL PRIVILEGES ON DATABASE ALTER TABLE <table_name> IF EXISTS Search for data
<db_name> TO <user_name>; ALTER <column_name> TYPE <data_type>
[<constraints>];
SELECT * FROM <table_name> WHERE
Grant connection permissions on database <column_name> = <value>;
Delete column
GRANT CONNECT ON DATABASE <db_name>
Insert data
TO <user_name>; ALTER TABLE <table_name> IF EXISTS
DROP <column_name>;
INSERT INTO <table_name> VALUES(
Grant permissions on schema <value_1>, <value_2> );
Update column to be an auto-incrementing
GRANT USAGE ON SCHEMA public TO primary key
Update data
<user_name>;
ALTER TABLE <table_name>
UPDATE <table_name>
Grant permissions to functions ADD COLUMN <column_name> SERIAL
SET <column_1> = <value_1>,
PRIMARY KEY;
<column_2> = <value_2>
GRANT EXECUTE ON ALL FUNCTIONS IN WHERE <column_1> = <value>;
SCHEMA public TO <user_name>; Insert into a table, with an auto-
incrementing primary key
Delete all data
Grant permissions to select, update, insert,
delete, on a all tables INSERT INTO <table_name>
DELETE FROM <table_name>;
VALUES (DEFAULT, <value1>);
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
Describe table with \ef [FUNC] Edit function definition \dn[S+] List schemas
\d+ <table>
details
\p Show the contents \di[S+] List indexes
List tables from current
\dt
schema Reset (clear) the query \du[+] List roles
\r
buffer
List tables from all \ds[S+] List sequences
\dt *.*
schemas Display history or save it to
\s [FILE]
file \df[antw][S+] List functions
\dt <schema>.* List tables for a schema
\w FILE Write query buffer to file \deu[+] List user mappings
List table access
\dp
privileges \dv[S+] List views
# 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.sq $ psql -U postgres -c 'SHOW config_f
-F Format (c: custom, d: directory, t: tar) -F Format (c: custom, d: directory, t: tar, $ sudo systemctl restart postgresql
p: plain text sql(default))
Use pg_dump -? to get the full list of Use pg_restore -? to get the full list of
options options
Import/Export CSV
# Also see
Posgres-cheatsheet (gist.github.com)
中文版 #Notes
Neo4j Cheatshee PyTorch Cheatshe Taskset Cheatshe
Quick Reference Quick Reference Quick Reference