Skip to content

Instantly share code, notes, and snippets.

@rcherara
Last active February 21, 2019 00:45
Show Gist options
  • Save rcherara/a90673bf565e784ce5a4013ebe649f82 to your computer and use it in GitHub Desktop.
Save rcherara/a90673bf565e784ce5a4013ebe649f82 to your computer and use it in GitHub Desktop.

Installing the PostgreSQL Client

$ brew install libpq

Psql

Psql is the interactive terminal for working with Postgres

  $ psql -h localhost -U username databasename
    * -h the host to connect to
    * -U the user to connect with
    * -p the port to connect to (default is 5432)
  $ psql "dbname=dbhere host=hosthere user=userhere password=pwhere port=5432 sslmode=require"

List tables in database

rcherara=# \d
                 List of relations
 Schema |        Name        |   Type   |  Owner
--------+--------------------+----------+----------
 public | customer           | table    | postgres
 public | hibernate_sequence | sequence | postgres
(2 rows)

Describe a table

rcherara=# \d customer
                       Table "public.customer"
  Column   |          Type          | Collation | Nullable | Default
-----------+------------------------+-----------+----------+---------
 id        | bigint                 |           | not null |
 firstname | character varying(255) |           |          |
 lastname  | character varying(255) |           |          |
Indexes:
    "customer_pkey" PRIMARY KEY, btree (id)

List all databases

rcherara-#  \l
                                   List of databases
     Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
--------------+----------+----------+-------------+-------------+-----------------------
 mydatabase   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 rcherara     | rcherara | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 reservations | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
              |          |          |             |             | postgres=CTc/postgres
 template1    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
              |          |          |             |             | postgres=CTc/postgres
(6 rows)

rcherara-#

Connect to another database

rcherara-# \c reservations
You are now connected to database "reservations" as user "rcherara".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment