PostgreSQL Cheat Sheet

Basic Commands

CommandDescription
psql [OPTION]… [DBNAME [USERNAME]]
psql -h <host> DBNAME USERNAME
Start the PostgreSQL command-line interface
createdb dbnameCreate a new database
use dbnameSwitch to use dbname
dropdb dbnameDelete a database
createuser usernameCreate a new user
dropuser usernameDelete a user
pg_dump dbname > filename.sqlBackup a database
psql -d dbname -f filename.sqlRestore a database

Tables

CommandDescription
CREATE TABLE tablename (column1 datatype1, column2 datatype2, ...)Create a new table
ALTER TABLE tablename ADD COLUMN column datatypeAdd a new column to a table
ALTER TABLE tablename DROP COLUMN columnRemove a column from a table
DROP TABLE tablenameDelete a table
SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE';List all tables in a database
SELECT table_name FROM information_schema.tables WHERE table_type=’BASE TABLE’;List all tables in all schemas, you can remove the “table_schema=’public'” condition

Data Manipulation

CommandDescription
INSERT INTO tablename (column1, column2, ...) VALUES (value1, value2, ...)Insert a new row into a table
UPDATE tablename SET column = value WHERE conditionUpdate one or more rows in a table
DELETE FROM tablename WHERE conditionDelete one or more rows from a table
SELECT column1, column2, ... FROM tablename WHERE conditionQuery data from a table

Querying Data

CommandDescription
SELECT * FROM tablenameRetrieve all rows and columns from a table
SELECT column1, column2, ... FROM tablenameRetrieve specific columns from a table
SELECT column1, column2, ... FROM tablename WHERE conditionRetrieve specific rows based on a condition
SELECT column1, COUNT(*) FROM tablename GROUP BY column1Group rows based on a column and count the number of rows in each group
SELECT column1, AVG(column2) FROM tablename GROUP BY column1Group rows based on a column and calculate the average value of another column in each group

Joins

CommandDescription
SELECT * FROM table1 JOIN table2 ON table1.column = table2.columnJoin two tables based on a common column
SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.columnLeft join two tables based on a common column
SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.columnRight join two tables based on a common column
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.columnFull outer join two tables based on a common column

Other Commands

CommandDescription
SELECT datname FROM pg_database;list all the databases in a PostgreSQL server
GRANT SELECT ON mytable TO myuser;grant SELECT privileges on a table called `mytable` to a user named `myuser`
GRANT SELECT, INSERT, UPDATE ON mytable TO myuser;grant multiple privileges at once
GRANT SELECT ON ALL TABLES IN SCHEMA myschema TO myuser;grant privileges on all tables in a schema, you can use the ALL TABLES keyword:
GRANT USAGE ON SCHEMA myschema TO myuser;grant privileges on a schema itself
SELECT table_name, grantee, privilege_type FROM information_schema.table_privileges WHERE table_schema = ‘myschema’;to view the privileges granted on all tables in a schema

Shortcut Commands

CommandDescription
\llist all the databases in a PostgreSQL server
\du+ myuserview the privileges granted to a specific user or role
\dp mytableview the privileges granted on a specific object, such as a table or view

Leave a Reply

Proudly powered by WordPress | Theme: Code Blog by Crimson Themes.