Programming/PostgreSQL/Databases: Difference between revisions

From Dev Wiki
Jump to navigation Jump to search
(Clean up page a bit)
Line 1: Line 1:
== Creating a New Database ==
{{ note | Unless otherwise specified, all commands from this section assume you're in the [[PostgreSQL#Basics | PostgreSQL shell]]. }}
=== Via Linux Shell ===
From the PostgreSQL shell:
CREATEDB <database_name>;


=== Via PostgreSQL Shell ===
== General Commands ==
=== Creating a New Database ===
  CREATE DATABASE <database_name>;
  CREATE DATABASE <database_name>;


== Deleting a Database ==
=== Deleting a Database ===
  DROP DATABASE <database_name>;
  DROP DATABASE <database_name>;


== Show All Databases ==
=== Show All Databases ===
From the PostgreSQL shell:
  \l
  \l


== Load Database ==
=== Load Database ===
  \c <database_name>
  \c <database_name>


== Give Database Privileges to User ==
=== Set Database Privileges ===
From the PostgreSQL shell:
  GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <user_name>;
  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>
=== Import Database from File ===
pg_restore -U <user_name> -d <database_name> <file_location>

Revision as of 04:41, 20 November 2020

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

General Commands

Creating a New Database

CREATE DATABASE <database_name>;

Deleting a Database

DROP DATABASE <database_name>;

Show All Databases

\l

Load Database

\c <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>

Import Database from File

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