What is psql
Psql is the interactive terminal for working with Postgres. Theres an abundance of flags available for use when working with psql, but lets focus on some of the most important ones, then how to connect:
- -h the host to connect to
- -U the user to connect with
- -p the port to connect to (default is 5432)
Connection
Connect to PostgreSQL
psql -h localhost -U username
Connect to database
psql -h localhost -U username databasename
Commonly used commands
List tables in database
# \d
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+-------
public | employees | table | craig
(1 row)
List all databases
# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------+-----------+----------+---------+-------+-----------------------------
learning | jarvis | UTF8 | C | UTF-8 |
Connect to another database
#\c dbname
Quit from postgres shell
#\q