- Learn
- PostgreSQL
- PostgreSQL SHOW TABLES equivalent
PostgreSQL SHOW TABLES equivalent
There is no SHOW TABLES in PostgreSQL. Use \dt in psql, or query information_schema.tables.
Quick answer
In psql:
From SQL, which is what you need from an application or a GUI:
SHOW TABLES is a MySQL command. PostgreSQL does not have it.
Why SHOW TABLES does not work
Type the MySQL command and PostgreSQL gives you something that looks unrelated to tables:
The reason is that SHOW is a real PostgreSQL command, it just does something different. It reads configuration parameters:
So PostgreSQL reads SHOW TABLES as a request for a setting called tables, does not find one, and says so. The syntax was fine. The parameter is what does not exist. SHOW DATABASES fails the same way, for the same reason.
Coming from MySQL
| MySQL | PostgreSQL (psql) | PostgreSQL (SQL) |
|---|---|---|
SHOW TABLES; | \dt | SELECT tablename FROM pg_tables WHERE schemaname='public' |
SHOW DATABASES; | \l | SELECT datname FROM pg_database |
DESCRIBE mytable; | \d mytable | query information_schema.columns |
SHOW COLUMNS FROM t; | \d t | query information_schema.columns |
SHOW CREATE TABLE t; | pg_dump -st t | no direct equivalent |
See PostgreSQL list databases for the SHOW DATABASES side.
Using \dt in psql
\dt shows tables in your current search_path, which by default means the public schema only. That is the single most common reason a table you know exists does not appear.
To see every schema:
To see one schema:
Add + for size and description:
Related listing commands:
| Command | Lists |
|---|---|
\dt | Tables |
\dv | Views |
\dm | Materialized views |
\di | Indexes |
\ds | Sequences |
\dn | Schemas |
\d | Tables, views, and sequences together |
\d mytable | The structure of one table |
Listing tables with SQL
Backslash commands are a psql feature. They do not work from an application driver, a GUI query editor, or anything that is not psql. For those, query the catalog.
Using pg_tables
Excluding those two schemas is what separates your tables from PostgreSQL's own. Without the filter you get about sixty system tables.
Using information_schema
information_schema is the SQL standard version, so the same query works on other databases:
table_type = 'BASE TABLE' excludes views. Drop it and you get both.
There is a catch worth knowing: information_schema only shows objects you have privileges on. pg_tables shows everything. If a table is missing from one and present in the other, that is why.
Tables in the current schema only
With row counts
Exact counts require scanning each table. For an estimate, the planner's statistics are free:
These are estimates maintained by autovacuum, not exact numbers. For an exact count of one table, SELECT count(*) FROM mytable is the only honest answer.
With table sizes
pg_total_relation_size includes indexes and TOAST data. pg_relation_size counts only the table itself, which is usually not the number you want.
Listing tables in a script
For shell scripts, use -A and -t to strip formatting:
In Docker:
Checking if a table exists
to_regclass is shorter and returns NULL rather than an error when the table is absent:
Common problems
My table is not listed
Almost always a schema issue. \dt only shows your search_path. Check where the table actually lives:
Then either qualify it (analytics.mytable), or add the schema to your path (SET search_path TO analytics, public;).
\dt says "Did not find any relations"
You are connected to a database that has no tables in your search path. Confirm which database you are in with \conninfo, because connecting to the wrong database looks identical to having no tables. See connecting to a PostgreSQL database.
\dt does not work in my app
It is a psql meta-command, not SQL. Use the pg_tables or information_schema queries above.
Quick reference
| Task | Command |
|---|---|
MySQL's SHOW TABLES | \dt |
| List tables (psql) | \dt |
| List tables in all schemas | \dt *.* |
| List tables with sizes | \dt+ |
| Describe a table | \d mytable |
| List tables (SQL) | SELECT tablename FROM pg_tables WHERE schemaname='public' |
| List tables (portable SQL) | SELECT table_name FROM information_schema.tables |
| Check a table exists | SELECT to_regclass('public.mytable') |
| Estimated row counts | SELECT relname, n_live_tup FROM pg_stat_user_tables |
| List views | \dv |
| List indexes | \di |