Using PSQL (Postgres CLI)

1 min read

Connecting

# connect to database pokemon as user postgres
psql pokemon postgres

pokemon=# \conninfo
You are connected to database "pokemon" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

Listing databases and tables

# List databases
postgres=# \l

# Connect to database
postgres=# \c pokemon

# List tables
# \dt+ is also an option
pokemon=# \dt
              List of relations
 Schema |       Name       | Type  |  Owner
--------+------------------+-------+----------
 public | _sqlx_migrations | table | postgres
 public | pokemon          | table | postgres
(2 rows)

Schema of a table

pokemon=# \d pokemon
                       Table "public.pokemon"
         Column          |  Type   | Collation | Nullable | Default
-------------------------+---------+-----------+----------+---------
 id                      | integer |           | not null |
 name                    | text    |           | not null |
 description             | text    |           |          |
 shakespeare_description | text    |           |          |
Indexes:
    "pokemon_pkey" PRIMARY KEY, btree (id)
    "pokemon_name" UNIQUE, btree (name)
    "pokemon_name_key" UNIQUE CONSTRAINT, btree (name)