Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

List Databases with \l

Open psql as the administrative role and type \l:

sudo -u postgres psql
\l

The output is a table with the name, owner, encoding, locale settings, and access privileges of each database. \l+ adds the size and the tablespace. A new server already has three entries:

  • postgres: an empty default database for administrative connections.
  • template1: the template every new database is copied from.
  • template0: a clean template that should never be changed.

List Databases from the Shell

sudo -u postgres psql -l

This prints the same table and exits. For a plain list that a script can read, ask for unaligned output with no headers:

sudo -u postgres psql -At -c "SELECT datname FROM pg_database WHERE NOT datistemplate;"

List Databases with SQL

The list lives in the system catalog pg_database, so any SQL client can read it:

SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname;

With sizes, largest first:

SELECT datname, pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
WHERE datistemplate = false
ORDER BY pg_database_size(datname) DESC;

If a database grows faster than expected, compare with the disk itself using how to check disk space in Linux.

Switch to a Database with \c

\c app

psql answers that you are now connected to database app, and the prompt changes to app=#. Unlike MySQL, PostgreSQL has no USE statement: a connection belongs to one database, and \c opens a new connection. To start psql in a database directly:

psql -d app -U app_user -h 127.0.0.1

\conninfo shows which database, user, and host the session uses.

List Tables with \dt

CommandShows
\dtTables in the search path
\dt+Tables with sizes
\dt app.*Tables in schema app
\dt *.*Tables in all schemas
\dnSchemas
\dvViews
\diIndexes

If \dt reports that it did not find any tables (or relations, in older psql versions), you are either in the wrong database or the tables are in a schema outside your search path. Check with \dn and \dt *.*.

Describe a Table with \d

\d orders

This lists the columns with their types, nullability, and defaults, followed by indexes, constraints, and foreign keys. \d+ orders adds storage details and the table comment.

List Tables with SQL

SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
  AND table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY 1, 2;

Table sizes including indexes:

SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) AS size
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;

Common Problems

  • Peer authentication failed: you ran psql as a Linux user without a matching role. Use sudo -u postgres psql, or connect with -h 127.0.0.1 and a role that has a password, where pg_hba.conf allows password logins.
  • Database does not exist: psql connects to a database named after the role when none is given. Add -d postgres or the right name.
  • Permission denied for a table: the role may connect but has no SELECT right; see how to create a user in PostgreSQL.

More commands are in the psql commands cheat sheet. The MySQL counterpart is how to list databases in MySQL.

Was this answer helpful?
Loading...