Postgres Cheatsheet
This is a collection of the most common commands I run while administering Postgres databases. The variables shown between the open and closed tags, “<” and “>”, should be replaced with a name you choose. Postgres has multiple shortcut functions, starting with a forward slash, “". Any SQL command that is not a shortcut, must end with a semicolon, “;”. You can use the keyboard UP and DOWN keys to scroll the history of previous commands you’ve run.
Setup
1
2
3
4
5
6
7
8
| sudo echo "deb http://apt.postgresql.org/pub/repos/apt/ wily-pgdg main" > \
/etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y postgresql-9.5 postgresql-client-9.5 postgresql-contrib-9.5
sudo su - postgres
psql
|
1
2
3
4
5
| psql
psql -U <username> -d <database> -h <hostname>
psql --username=<username> --dbname=<database> --host=<hostname>
|
disconnect
clear the screen
info
1
2
| sudo nano $(locate -l 1 main/postgresql.conf)
sudo service postgresql restart
|
debug logs
1
2
| # print the last 24 lines of the debug log
sudo tail -24 $(find /var/log/postgresql -name 'postgresql-*-main.log')
|
Recon
show version
show system status
show environmental variables
list users
1
| SELECT rolname FROM pg_roles;
|
show current user
show current user’s permissions
list databases
show current database
1
| SELECT current_database();
|
show all tables in database
list functions
Databases
list databasees
connect to database
show current database
1
| SELECT current_database();
|
1
| CREATE DATABASE <database_name> WITH OWNER <username>;
|
1
| DROP DATABASE IF EXISTS <database_name>;
|
1
| ALTER DATABASE <old_name> RENAME TO <new_name>;
|
Users
list roles
1
| SELECT rolname FROM pg_roles;
|
1
| CREATE USER <user_name> WITH PASSWORD '<password>';
|
1
| DROP USER IF EXISTS <user_name>;
|
1
| ALTER ROLE <user_name> WITH PASSWORD '<password>';
|
Permissions
become the postgres user, if you have permission errors
1
2
| sudo su - postgres
psql
|
grant all permissions on database
1
| GRANT ALL PRIVILEGES ON DATABASE <db_name> TO <user_name>;
|
grant connection permissions on database
1
| GRANT CONNECT ON DATABASE <db_name> TO <user_name>;
|
grant permissions on schema
1
| GRANT USAGE ON SCHEMA public TO <user_name>;
|
grant permissions to functions
1
| GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO <user_name>;
|
grant permissions to select, update, insert, delete, on a all tables
1
| GRANT SELECT, UPDATE, INSERT ON ALL TABLES IN SCHEMA public TO <user_name>;
|
grant permissions, on a table
1
| GRANT SELECT, UPDATE, INSERT ON <table_name> TO <user_name>;
|
grant permissions, to select, on a table
1
| GRANT SELECT ON ALL TABLES IN SCHEMA public TO <user_name>;
|
Schema
list schemas
1
2
3
4
5
| \dn
SELECT schema_name FROM information_schema.schemata;
SELECT nspname FROM pg_catalog.pg_namespace;
|
1
| CREATE SCHEMA IF NOT EXISTS <schema_name>;
|
1
| DROP SCHEMA IF EXISTS <schema_name> CASCADE;
|
Tables
list tables, in current db
1
2
3
| \dt
SELECT table_schema,table_name FROM information_schema.tables ORDER BY table_schema,table_name;
|
list tables, globally
1
2
3
| \dt *.*.
SELECT * FROM pg_catalog.pg_tables
|
list table schema
1
2
3
4
5
6
| \d <table_name>
\d+ <table_name>
SELECT column_name, data_type, character_maximum_length
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = '<table_name>';
|
1
2
3
4
| CREATE TABLE <table_name>(
<column_name> <column_type>,
<column_name> <column_type>
);
|
create table, with an auto-incrementing primary key
1
2
3
| CREATE TABLE <table_name> (
<column_name> SERIAL PRIMARY KEY
);
|
1
| DROP TABLE IF EXISTS <table_name> CASCADE;
|
Columns
1
2
| ALTER TABLE <table_name> IF EXISTS
ADD <column_name> <data_type> [<constraints>];
|
update column
1
2
| ALTER TABLE <table_name> IF EXISTS
ALTER <column_name> TYPE <data_type> [<constraints>];
|
delete column
1
2
| ALTER TABLE <table_name> IF EXISTS
DROP <column_name>;
|
update column to be an auto-incrementing primary key
1
2
| ALTER TABLE <table_name>
ADD COLUMN <column_name> SERIAL PRIMARY KEY;
|
insert into a table, with an auto-incrementing primary key
1
2
3
4
5
6
| INSERT INTO <table_name>
VALUES (DEFAULT, <value1>);
INSERT INTO <table_name> (<column1_name>,<column2_name>)
VALUES ( <value1>,<value2> );
|
Data
1
| SELECT * FROM <table_name>;
|
read one row of data
1
| SELECT * FROM <table_name> LIMIT 1;
|
search for data
1
| SELECT * FROM <table_name> WHERE <column_name> = <value>;
|
1
| INSERT INTO <table_name> VALUES( <value_1>, <value_2> );
|
1
2
3
| UPDATE <table_name>
SET <column_1> = <value_1>, <column_2> = <value_2>
WHERE <column_1> = <value>;
|
1
| DELETE FROM <table_name>;
|
delete specific data
1
2
| DELETE FROM <table_name>
WHERE <column_name> = <value>;
|
Scripting
1
2
3
| psql -U <username> -d <database> -h <host> -f <local_file>
psql --username=<username> --dbname=<database> --host=<host> --file=<local_file>
|
backup database data, everything
1
2
3
| pg_dump <database_name>
pg_dump <database_name>
|
backup database, only data
1
2
3
| pg_dump -a <database_name>
pg_dump --data-only <database_name>
|
backup database, only schema
1
2
3
| pg_dump -s <database_name>
pg_dump --schema-only <database_name>
|
1
2
3
| pg_restore -d <database_name> -a <file_pathway>
pg_restore --dbname=<database_name> --data-only <file_pathway>
|
restore database schema
1
2
3
| pg_restore -d <database_name> -s <file_pathway>
pg_restore --dbname=<database_name> --schema-only <file_pathway>
|
1
| \copy <table_name> TO '<file_path>' CSV
|
export table, only specific columns, to CSV file
1
| \copy <table_name>(<column_1>,<column_1>,<column_1>) TO '<file_path>' CSV
|
1
| \copy <table_name> FROM '<file_path>' CSV
|
import CSV file into table, only specific columns
1
| \copy <table_name>(<column_1>,<column_1>,<column_1>) FROM '<file_path>' CSV
|
Debugging
Using EXPLAIN
Error Reporting and Logging