PostgreSQL Cheat Sheet Query Page

Posted on

This is a cheat sheet command page for everything related to PostgreSQL Database Server.

List all existing database :

postgres=# \l

or

postgres=# \list

List all roles with the privileges :

postgres=# \du

List all role names :

postgres=# SELECT rolname FROM pg_roles;

Quit from PostgreSQL Command Console :

postgres=# \q

or

postgres=# \quit

Create Database :

postgres=# create database db_name

Drop Database :

postgres=# drop database db_name

Restore Database from a plain file backup database :

psql -Upostgres_username db_name < db_dump_file_location_and_name

psql -Upostgres_username db_name < db_dump_file_location_and_name.dump 

Restore Database from a PostgreSQL custom-format dump file backup database :

pg_restore -Upostgres_username -d db_name db_dump_file_location_and_name.pgdump

Select and Use Database :

postgres=# \connect db_name

or

postgres=# \c db_name

Show All Tables in a Database :

dbname=# \d

or

dbname=# \dt

Create User in PostgreSQL Database Server

postgres=# CREATE USER username WITH PASSWORD 'password'

Connect to a database

postgres=# \c database_name

Leave a Reply