PostgreSQL Basic Commands

1. Install PostgreSQL

sudo apt install postgresql

2. Entering the command

su - postgres  # Switch user to postgres
psql           # Enter into PostgreSQL

3. Basic commands:

\?                   => List all the commands
\l                   => List databases
\conninfo            => Display information about current connection
\c [DBNAME]          => Connect to new database, e.g., \c template1
\dt                  => List tables of the public schema
\dt <schema-name>.*  => List tables of certain schema, e.g., \dt public.*
\dt *.*              => List tables of all schemas
\q                   => Quit psql

4. Create a database

CREATE DATABASE mydatabase ENCODING 'UTF-8';

5. Create a user

CREATE USER myuser WITH ENCRYPTED PASSWORD 'mysecretpassword';

6. Grant privileges to user on a database

GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;

7. Quite PostgreSQL

\q # Backslash and q to quit