This is a draft cheat sheet. It is a work in progress and is not finished yet.
psql client options
|
List all available databases |
|
Connect to database |
|
Connect with user |
|
Connect with password |
|
Connect to host |
|
Connect via port |
|
Run command |
|
Run file |
|
Output query output to log file and stdout |
|
Output query output to log file |
|
Print tuples only (no headers/footers) |
psql commands
Command overview |
|
Exit |
|
Connect to database |
|
Show table definition |
|
List tables |
|
List databases |
|
List schemas |
|
List functions |
|
List views |
|
Show function SQL code |
|
Pretty-format query results |
|
Query analysis
See the query plan for the given query |
EXPLAIN __query__ |
See and execute the query plan for the given query |
EXPLAIN ANALYZE __query__ |
Collect statistics |
ANALYZE [__table__] |
Get statistics for table |
SELECT * from pg_stat_user_tables where relname=__table__ |
Get statistics for table in current transaction |
SELECT * from pg_stat_xact_user_tables where relname=__table__ |
DDL
Create database |
CREATE DATABASE <database_name> WITH OWNER <username>;
|
Delete database |
DROP DATABASE IF EXISTS <database_name>;
|
Rename database |
ALTER DATABASE <old_name> RENAME TO <new_name>;
|
Create table |
CREATE TABLE <table_name>( <column_name> SERIAL PRIMARY KEY, <column_name> <column_type> );
|
Delete table |
ROP TABLE IF EXISTS <table_name> CASCADE;
|
Add column |
ALTER TABLE <table_name> IF EXISTS ADD <column_name> <data_type> [<constraints>];
|
Alter column |
ALTER TABLE <table_name> IF EXISTS ALTER <column_name> TYPE <data_type> [<constraints>];
|
Delete column |
ALTER TABLE <table_name> IF EXISTS DROP <column_name>;
|
Constraints
Add primary key |
ALTER TABLE __table__ ADD CONSTRAINT __constraintname__ PRIMARY KEY(__col__, __col__); |
Add unique |
ALTER TABLE __table__ ADD CONSTRAINT __constraintname__ UNIQUE(__col__, __col__); |
Add foreign key |
ALTER TABLE __table__ ADD CONSTRAINT __constraintname__ FOREIGN KEY(__col__, __col__) REFERENCES __table__(__col__, __col__); |
Add check |
ALTER TABLE __table__ ADD CONSTRAINT __constraintname__ CHECK(__condition__); |
Drop |
ALTER TABLE __table__ DROP CONSTRAINT __constraint__; |
|
|
Handy queries
List procedure/function |
SELECT * FROM pg_proc WHERE proname='__procedurename__'
|
Show DB table space in use |
SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));
|
Show DB space in use |
SELECT pg_size_pretty(pg_database_size('__database_name__'));
|
Show current user's statement timeout |
show statement_timeout;
|
Queries being executed |
SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query FROM pg_stat_activity WHERE datname='__database_name__';
|
Get all queries from all dbs waiting for data |
SELECT * FROM pg_stat_activity WHERE waiting='t';
|
Currently running queries with process pid |
SELECT pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_activity(s.backendid) AS current_query FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
|
Users
Add user |
ADD USER __user__; |
Drop user |
DROP USER __user__; |
Add user with password |
ADD USER __user__ WITH PASSWORD __password__; |
Grant permissions for table |
GRANT INSERT, UPDATE, DELETE ON TABLE __table__ TO __user__; |
Grant permissions for all tables |
GRANT INSERT, UPDATE, DELETE ON ALL TABLES TO __user__; |
Grant permissions for sequence |
GRANT USAGE ON SEQUENCE __sequence__ TO __user__; |
Grant permissions for all sequences |
GRANT USAGE ON ALL SEQUENCES TO __user__; |
Grant permissions for function |
GRANT EXECUTE ON FUNCTION __function__ TO __user__; |
Grant permissions for all functions |
GRANT EXECUTE ON ALL FUNCTIONS TO __user__; |
Common Functions
Current date |
current_date |
Current time |
current_time |
Current timestamp |
localtimestamp |
Get date part of column |
date(__col__) |
Get time part of column |
__col__::time |
Add interval to timestamp |
__col__ + interval '1 hour' |
Return serial number |
INSERT ... RETURNING LASTVAL(); |
Conditional expressions |
CASE expression WHEN value THEN result [WHEN ...] [ELSE result] END |
Null values |
COALESCE(__value__, __default1__, __default2__) |
Cast |
__value__::__type__ |
Execute function returning value |
SELECT __function__; |
Execute function returning table |
SELECT * FROM __function__; |
|