Skip to content

Useful psql commands

Michael Hulse edited this page May 19, 2017 · 5 revisions

Switch to user:

$ sudo -i -u databasename

… fire up the psql prompt:

$ psql

You can connect to a specific database like so:

$ psql -d postgres

PostgreSQL version:

# SELECT version();

You can view your databases using:

# \l

Show all functions:

# \df databasename.*

Show details of a function:

# \sf+ functionName

Connect to databse:

# \c awarehealth

List all tables for an existing database you are already connected to:

# \dt

List schema tables:

# \dt+

Show the types along with their schema:

# \dT+

See all the types in schema foo:

# \dT+ foo.*

List all schemas with:

# \dn

Show all columns for a given table including type information, references and key constraints:

# \d [table_name]

List users:

# \du

If you’re all done, you can exit using:

# \q
Clone this wiki locally