Postgres Cheat Sheet

Techie     July 2022


1 . Connect to postgresql

a). First Option

$ sudo -u postgres psql

a). Second Option

# switch user
$ sudo su - postgres

# connect to postgresql
$ psql


2 . Exit postgres

\q


3 . List databases

\l

# include details like description, tablespace & DB size
\l+


4 . Connect to a database

\c database_name


5 . Create a database

create database database_name


6 . Delete a database

drop database database_name


7 . List tables

\dt


8 . Describe table

\d table_name

# include more details
\d+ table_name


9 . List users and their roles

\du


10 . Show current user

select current_user;


11 . Show connection information

\conninfo


12 . Rename a database

alter database current_db_name rename to new_db_name;


13 . Grant all permissions on database to user

grant all privileges on database db_name to user_name;


14 . List tables for a schema

\dt name_of_schema.*


15 . Add a comment on a table

Comment on table user is 'App tenant records';


16 . Add a comment on a column

Comment on column user.pin is 'Tenant tax identification Number';


17 . Copy table data to CSV file

\copy (SELECT * FROM table_name) TO 'file_path_and_name.csv' WITH CSV


18 . Execute commands from a file

psql mydb -f file.sql


19 . Connect remote PostgreSQL

psql -U user_here -h host_ip_here -p port_here -d db_name_here

# example
psql -U admin -h 35.21.09.12 -p 2506 -d mydb


20 . Generate HTML report

psql -c "\l+" -H postgres > database.html


21 . Print the psql version

psql -V


22 . Force password

psql -W db_name


23 . Connect to a host/port

psql -h localhost -p 5432 db_name


24 . Connect as a specific user

psql -U user_name database_name


25 . Backup all databases

pg_dumpall -U postgres > all.sql


26 . Backup a database

pg_dump -d db_name -f mydb_backup.sql

# Important flags

-a   Dump only the data, not the schema
-s   Dump only the schema, no data
-c   Drop database before recreating
-C   Create database before restoring
-t   Dump the named table(s) only
-F   Format (c: custom, d: directory, t: tar)


27 . Restore a database

psql -U user db_name < mydb_backup.sql

# alternative

pg_restore -d db_name mydb_backup.sql -c

# Important flags

-U   Specify a database user
-c   Drop database before recreating
-C   Create database before restoring
-e   Exit if an error has encountered
-F   Format (c: custom, d: directory, t: tar, p: plain text sql(default))


28 . Import CSV file into table

\copy table FROM '<path>' CSV
\copy table(col1,col1) FROM '<path>' CSV


29 . List table access privileges

\dp


30 . Change user password

# a).

# 1. Login into the psql:

$ sudo -u postgres psql


# 2. Then in the psql console change the password and quit:

postgres=# \password postgres
Enter new password: <new-password>
postgres=# \q

# or 

# b). 

# 1. To log in without a password:

sudo -u user_name psql db_name

# 2. To reset the password if you have forgotten:

ALTER USER user_name WITH PASSWORD 'new_password';


Thanks for reading, see you in the next one!