Skip to content

Instantly share code, notes, and snippets.

@anistark
Last active January 10, 2018 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anistark/6d0ab8618572fee495333923aba86cac to your computer and use it in GitHub Desktop.
Save anistark/6d0ab8618572fee495333923aba86cac to your computer and use it in GitHub Desktop.
Postgres Most used commands

Postgres

Run :

psql -d test -U postgres -W

Most frequently used commands :

Desciption Command
Show all databases \l
Select a database to work-on \c dbname
Login as superuser sudo -u postgres psql
Show structure of table \d+ tablename
Quit Postgres cli mode \quit
Terminate all open sessions SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pid <> pg_backend_pid()
Export database pg_dump db_name > file_name.sql
Export database pg_restore -U postgres -d db_name -v file_name.sql
Import database psql -d db_name -U postgres -f file_name.sql
Write Data from database to CSV File Copy (select * from table where column = id) To 'file_name.csv' With CSV DELIMITER ',';

Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment