List Databases with \l
Open psql as the administrative role and type \l:
sudo -u postgres psql
\lThe 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 -lThis 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_, 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 apppsql 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
| Command | Shows |
|---|---|
\dt | Tables in the search path |
\dt+ | Tables with sizes |
\dt app.* | Tables in schema app |
\dt *.* | Tables in all schemas |
\dn | Schemas |
\dv | Views |
\di | Indexes |
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 ordersThis 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
psqlas a Linux user without a matching role. Usesudo -u postgres psql, or connect with-h 127.0.0.1and a role that has a password, wherepg_allows password logins.hba.conf - Database does not exist: psql connects to a database named after the role when none is given. Add
-d postgresor the right name. - Permission denied for a table: the role may connect but has no
SELECTright; 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.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations