Skip to content

Instantly share code, notes, and snippets.

@nd3w
Last active July 4, 2024 00:47
Show Gist options
  • Save nd3w/bfd5749b28ebbc70340efdc0b027a8cc to your computer and use it in GitHub Desktop.
Save nd3w/bfd5749b28ebbc70340efdc0b027a8cc to your computer and use it in GitHub Desktop.
Some essential PostgreSQL commands

List databases:

\l

Use a database:

\c db_name

Use schema other than default 'public':

SET search_path = schema_name

List all schema in a database:

select schema_name from information_schema.schemata order by schema_name;

Drop schema in a database:

DROP SCHEMA IF EXISTS schema_name

Show tables in a database:

\dt

Export a database (full path Windows):

pg_dump -U postgres db_name > D:\Downloads\dump.sql

Export a database (full path Linux):

pg_dump -U postgres db_name > /home/username/bak/dump.sql

Export a database (into current directory):

pg_dump -U postgres db_name > dump.sql

Import a full database:

pg_restore -U postgres -d db_name -1 -f dump.sql

Change admin/root user's password:

sudo -u postgres psql
postgres=# \password postgres
Enter new password for user "postgres":
Enter it again:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment