What's the equivalent to show tables (from MySQL) in PostgreSQL?
How to show tables in PostgreSQL?
2.8m views Asked by flybywire AtThere are 29 answers
On
(For completeness)
You could also query the (SQL-standard) information schema:
SELECT
table_schema || '.' || table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
On
Running psql with the -E flag will echo the query used internally to implement \dt and similar:
sudo -u postgres psql -E
postgres=# \dt
********* QUERY **********
SELECT n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;
**************************
On
You can use PostgreSQL's interactive terminal Psql to show tables in PostgreSQL.
1. Start Psql
Usually you can run the following command to enter into psql:
psql DBNAME USERNAME
For example, psql template1 postgres
One situation you might have is: suppose you login as root, and you don't remember the database name. You can just enter first into Psql by running:
sudo -u postgres psql
In some systems, sudo command is not available, you can instead run either command below:
psql -U postgres
psql --username=postgres
2. Show tables
Now in Psql you could run commands such as:
\?list all the commands\llist databases\conninfodisplay information about current connection\c [DBNAME]connect to new database, e.g.,\c template1\dtlist tables of the public schema\dt <schema-name>.*list tables of certain schema, e.g.,\dt public.*\dt *.*list tables of all schemas- Then you can run SQL statements, e.g.,
SELECT * FROM my_table;(Note: a statement must be terminated with semicolon;) \qquit psql
On
Note that \dt alone will list tables in the public schema of the database you're using. I like to keep my tables in separate schemas, so the accepted answer didn't work for me.
To list all tables within a specific schema, I needed to:
1) Connect to the desired database:
psql mydb
2) Specify the schema name I want to see tables for after the \dt command, like this:
\dt myschema.*
This shows me the results I'm interested in:
List of relations
Schema | Name | Type | Owner
----------+-----------------+-------+----------
myschema | users | table | postgres
myschema | activity | table | postgres
myschema | roles | table | postgres
On
Login as a superuser so that you can check all the databases and their schemas:-
sudo su - postgres
Then we can get to postgresql shell by using following command:-
psql
You can now check all the databases list by using the following command:-
\l
If you would like to check the sizes of the databases as well use:-
\l+
Press q to go back.
Once you have found your database now you can connect to that database using the following command:-
\c database_name
Once connected you can check the database tables or schema by:-
\d
Now to return back to the shell use:-
q
Now to further see the details of a certain table use:-
\d table_name
To go back to postgresql_shell press \q.
And to return back to terminal press exit.
On
as a "quick oneliner"
# how-to list all the tables
export PGUSER='postgres'
export PGHOST='postgres-host-end-point'
export PGPORT=5432
export PGDATABASE=foobar
PGPASSWORD='uber-secret' psql -d $PGDATABASE -t -q -c \
"SELECT table_catalog,table_schema,table_name
FROM information_schema.tables where table_schema='public';
or if you prefer much clearer json output multi-liner :
IFS='' read -r -d '' sql_code <<"EOF_CODE"
select array_to_json(array_agg(row_to_json(t))) from (
SELECT table_catalog,table_schema,table_name
FROM information_schema.tables
ORDER BY table_schema,table_name ) t
EOF_CODE
psql -d postgres -t -q -c "$sql_code"|jq
On
\dt will work. And the equivalence of it is
SELECT
n.nspname as "Schema",
c.relname as "Name",
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM
pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE
c.relkind IN ('r', 'p', '')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname ! ~ '^pg_toast'
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1, 2;
On
These list all tables of all schemas of the current database:
\dt *.*
\dtS *.*
These list all tables of all schemas of the current database in detail:
\dt+ *.*
\dtS+ *.*
These list all tables of pg_catalog and public schemas of the current database:
\dtS
\dtS *
\dt *
These list all tables of pg_catalog and public schemas of the current database in detail:
\dtS+
\dtS+ *
\dt+ *
This lists all tables of public schema of the current database:
\dt
This lists all tables of public schema of the current database in detail:
\dt+
These list all tables of my_schema schema of the current database:
\dtS my_schema.*
\dt my_schema.*
These list all tables of my_schema schema of the current database in detail:
\dtS+ my_schema.*
\dt+ my_schema.*
On
Also note this from the pgAdmin documentation:
pgAdmin 4 provides dialogs that allow you to modify all table properties and attributes.
To access a dialog that allows you to create a database object, right-click on the object type in the pgAdmin tree control, and select the Create option for that object. For example, to create a new table, Select a database from the tree control, select the schema under the database, right-click on the Tables node, and select Create Table.
So once you've done that (or you could also use SQL commands and the pSQL client) and you have some tables, you can view them by expanding the "Schemas" object in the pgAdmin UI. Here's a screenshot of what it looks like:
After selecting a table, you may also click on "View Data" at the top toolbar (the one with the grid table icon) to view the table's records.

From the
psqlcommand line interface,First, choose your database
Then, this shows all tables in the current schema:
Programmatically (or from the
psqlinterface too, of course):The system tables live in the
pg_catalogdatabase.