Programming/PostgreSQL/Databases: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Organize page)
(Add optional command flags)
Line 22: Line 22:
=== Export Database to file ===
=== Export Database to file ===
  pg_dump -U <user_name> -d <database_name> <file_location>
  pg_dump -U <user_name> -d <database_name> <file_location>
Optional Flags:
* {{ ic |-h <host>}} - Allows specification of host, such as for connecting to a remote server.
* {{ ic |-p <port>}} - Allows specification of port, such as for non-standard installations that don't use the default port {{ ic |5432}}.


=== Import Database from File ===
=== Import Database from File ===
  pg_restore -U <user_name> -d <database_name> <file_location>
  pg_restore -U <user_name> -d <database_name> <file_location>
Optional Flags:
* {{ ic |-h <host>}} - Allows specification of host, such as for connecting to a remote server.
* {{ ic |-p <port>}} - Allows specification of port, such as for non-standard installations that don't use the default port {{ ic |5432}}.

Revision as of 07:14, 20 November 2020

Note: Unless otherwise specified, all commands from this page assume you're in the PostgreSQL shell.

General Commands

Show All Databases

\l

Create a New Database

CREATE DATABASE <database_name>;

Load a Database

\c <database_name>

Delete a Database

Warn: Note that this action cannot be undone.
DROP DATABASE <database_name>;

Set Database Privileges

GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <user_name>;

Database Backups

Export Database to file

pg_dump -U <user_name> -d <database_name> <file_location>

Optional Flags:

  • -h <host> - Allows specification of host, such as for connecting to a remote server.
  • -p <port> - Allows specification of port, such as for non-standard installations that don't use the default port 5432.

Import Database from File

pg_restore -U <user_name> -d <database_name> <file_location>

Optional Flags:

  • -h <host> - Allows specification of host, such as for connecting to a remote server.
  • -p <port> - Allows specification of port, such as for non-standard installations that don't use the default port 5432.