2399

What's the equivalent to show tables (from MySQL) in PostgreSQL?

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
flybywire
  • 261,858
  • 191
  • 397
  • 503

30 Answers30

3431

From the psql command line interface,

First, choose your database

\c database_name

Then, this shows all tables in the current schema:

\dt

Programmatically (or from the psql interface too, of course):

SELECT * FROM pg_catalog.pg_tables;

The system tables live in the pg_catalog database.

zwcloud
  • 4,546
  • 3
  • 40
  • 69
Mihai Limbășan
  • 64,368
  • 4
  • 48
  • 59
  • 127
    @StephenCorwin No, `\l` is the equivalent of `show databases` in MySQL. `dt` ≃ `show tables` and `l` ≃ `show databases` – user454322 Aug 24 '12 at 04:01
  • 17
    `\dt` is very useful. That `pg_catalog.pg_tables` one is much less so, as it appears to lump internal tables together with the user-created ones for whatever database you happen to be connected to. – aroth Jul 29 '13 at 06:25
  • 35
    `psql my_db_name` should be run in order `\dt` to work. When I ran `psql` without a database name, I got a "No relations found" message – Maksim Dmitriev Nov 19 '13 at 15:01
  • 45
    Without system tables: `SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'` – Matt White Jul 06 '14 at 00:36
  • 46
    You first need `\c ` to choose your database. – danio Sep 26 '16 at 15:40
  • 8
    To remember this, think `\dt` == Display Tables. – aaron-coding Mar 21 '17 at 19:08
  • Sounds useful to me to add that the result has to be [q]uit with the `q` key – Balmipour Sep 11 '17 at 20:03
  • 3
    You also need to specify the schema to use, e.g.: \dt my_schema.* – Ville Myrskyneva Dec 19 '17 at 07:12
  • This answer is incomplete. Please add the answer below ... \c etc. – mathtick Jan 09 '19 at 14:07
  • Error: no such table: pg_catalog.pg_tables – Matej J Jul 16 '20 at 07:44
  • You can also show more information about a specific table in the database using `\d++ TABLE_NAME;`, but first, you need to be in the specific database you need to see the table in using `\c DATABASE_NAME`. – Abdel-Raouf Mar 10 '21 at 12:38
  • Is there an explanation somewhere why the psql syntax is so unorthodox for an SQL DB client? I wonder what's the reasoning behind these backslashes, did they do it on purpose? I've to google it but to no avail. – salmin Sep 08 '22 at 14:01
  • Also, output of `Did not find any relations.` just means there are no tables in the schema. – NULL pointer Jul 08 '23 at 05:38
225

Login as superuser:

sudo -u postgres psql

You can list all databases and users by \l command, (list other commands by \?).

Now if you want to see other databases you can change user/database by \c command like \c template1, \c postgres postgres and use \d, \dt or \dS to see tables/views/etc.

JLarky
  • 9,833
  • 5
  • 36
  • 37
218

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:

  1. \? list all the commands
  2. \l list databases
  3. \conninfo display information about current connection
  4. \c [DBNAME] connect to new database, e.g., \c template1
  5. \dt list tables of the public schema
  6. \dt <schema-name>.* list tables of certain schema, e.g., \dt public.*
  7. \dt *.* list tables of all schemas
  8. Then you can run SQL statements, e.g., SELECT * FROM my_table;(Note: a statement must be terminated with semicolon ;)
  9. \q quit psql
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Yuci
  • 27,235
  • 10
  • 114
  • 113
162

(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');
Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
  • 3
    +1 although for completeness, mysql show tables only shows the current schema, its good to think of it this way, mysql only has one database but multiple schemas, where postgresql can have mutliple databases (catalogs) and schemas. So the equiv should be table_schema='DB_NAME'; – Rahly May 28 '14 at 18:18
  • Not exactly standard sql, cannot use "||" to concatenate strings on mssql – ChRoNoN Apr 08 '19 at 20:06
  • 6
    @ChRoNoN: that **is** standard SQL . `||` has been the string concatenation operator in the SQL standard since 1983 - it's MS SQL that uses a non-standard string concatenation operator. –  Aug 27 '20 at 09:00
56
  1. First login as postgres user:

    sudo su - postgres

  2. connect to the required db: psql -d databaseName

  3. \dt would return the list of all table in the database you're connected to.

nish
  • 6,952
  • 18
  • 74
  • 128
55

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.

Shashishekhar Hasabnis
  • 1,636
  • 1
  • 15
  • 36
44

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;        
**************************
bsb
  • 1,847
  • 26
  • 24
  • 2
    BTW, TOAST is used to store large values: http://www.postgresql.org/docs/8.3/static/storage-toast.html – Dorian Feb 24 '15 at 16:55
26

If you only want to see the list of tables you've created, you may only say:

\dt

But we also have PATTERN which will help you customize which tables to show. To show all including pg_catalog Schema, you can add *.

\dt *

If you do: \?

\dt[S+] [PATTERN] list tables

Community
  • 1
  • 1
Banned_User
  • 973
  • 2
  • 9
  • 17
26

use only see a tables

=> \dt

if want to see schema tables

=>\dt+

if you want to see specific schema tables

=>\dt schema_name.* 
Aryan
  • 391
  • 5
  • 5
  • I'm pretty sure you're confusing `+` with `S`. The latter (the letter) shows schema tables. The `+` simply shows extra information. – Garret Wilson Jan 15 '18 at 23:16
20

If you are using pgAdmin4 in PostgreSQL, you can use this to show the tables in your database:

select * from information_schema.tables where table_schema='public';
Reynante Daitol
  • 489
  • 4
  • 13
20

(MySQL) shows tables list for current database

show tables;

(PostgreSQL) shows tables list for current database

select * from pg_catalog.pg_tables where schemaname='public';
u tyagi
  • 732
  • 9
  • 18
19

First Connect with the Database using following command

\c database_name

And you will see this message - You are now connected to database database_name. And them run the following command

SELECT * FROM table_name;

In database_name and table_name just update with your database and table name

J4cK
  • 30,459
  • 8
  • 42
  • 54
  • 24
    I'm not sure this is answering the question. I think the OP is (was) trying to know all the tables in his database, not get all the rows from a particular table in his database... right? – snuggles Oct 30 '14 at 14:32
15
select 
  * 
from 
  pg_catalog.pg_tables 
where 
  schemaname != 'information_schema' 
  and schemaname != 'pg_catalog';
Bikramjeet Singh
  • 681
  • 1
  • 7
  • 22
Abdelhak
  • 176
  • 2
  • 12
14

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
rotarydial
  • 2,181
  • 2
  • 23
  • 27
12

\dt will list tables, and \pset pager off shows them in the same window, without switching to a separate one. Love that feature to death in dbshell.

lauri108
  • 1,381
  • 1
  • 13
  • 22
12

Those steps worked for me with PostgreSQL 13.3 and Windows 10

  1. Open cmd and type psql -a -U [username] -p [port] -h [server]
  2. Type \c [database] to connect to the database
  3. Type \dt or \d to show all tables
Anis KCHAOU
  • 830
  • 1
  • 11
  • 11
9

\dt (no * required) -- will list all tables for an existing database you are already connected to. Also useful to note:

\d [table_name] -- will show all columns for a given table including type information, references and key constraints.

user4642897
  • 91
  • 1
  • 2
8

The most straightforward way to list all tables at command line is, for my taste :

psql -a -U <user> -p <port> -h <server> -c "\dt"

For a given database just add the database name :

psql -a -U <user> -p <port> -h <server> -c "\dt" <database_name>

It works on both Linux and Windows.

Alain Cherpin
  • 305
  • 4
  • 5
  • command="\d+" psql_command="psql --no-password -d "ch_api_db" -U "ch_api_user" -c "${command}"" docker exec -it "${container_name}" sh -c "${psql_command}" gives me an error about syntax error near d – PirateApp Aug 17 '23 at 13:53
8

This SQL Query works with most of the versions of PostgreSQL and fairly simple .

select table_name from information_schema.tables where table_schema='public' ;
  • Don't post duplicate answers. – Jason Aller Aug 25 '20 at 14:54
  • 1
    it isn't duplicate there is small change this directly gives the table name , i tired to edit the original answer but it wasn't approved hence gave an answer which works – Vineet Kumar Gupta Aug 26 '20 at 09:27
  • 4
    The answer by Milen A. Radev provides the table_name. The answer by Reynante Daitol contains the rest of this code. If you believe that this code offers something new and unique that is a reason to include an explanation that points that out. Without the explanation people are left guessing why it is different or potentially better. – Jason Aller Aug 26 '20 at 14:09
7

You can list the tables in the current database with \dt.

Fwiw, \d tablename will show details about the given table, something like show columns from tablename in MySQL, but with a little more information.

JohnK
  • 6,865
  • 8
  • 49
  • 75
6
  1. In PostgreSQL command-line interface after login, type the following command to connect with the desired database.

        \c [database_name]
    

Then you will see this message You are now connected to database "[database_name]"

  1. Type the following command to list all the tables.

        \dt
    
Mohammad Ameen
  • 123
  • 4
  • 8
6

shows tables list for current database

SELECT * FROM pg_catalog.pg_tables;

Viettel Solutions
  • 1,519
  • 11
  • 22
5

Using psql : \dt

Or:

SELECT c.relname AS Tables_in FROM pg_catalog.pg_class c
        LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE pg_catalog.pg_table_is_visible(c.oid)
        AND c.relkind = 'r'
        AND relname NOT LIKE 'pg_%'
ORDER BY 1
MisterJoyson
  • 309
  • 4
  • 4
5

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
Yordan Georgiev
  • 5,114
  • 1
  • 56
  • 53
4

First of all you have to connect with your database like

my database is ubuntu

use this command to connect

 \c ubuntu

This message will show

"You are now connected to database "ubuntu" as user "postgres"."

Now

Run this command to show all tables in it

\d+
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Usman Yaqoob
  • 535
  • 5
  • 13
1

\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;
iChux
  • 2,266
  • 22
  • 37
0

To view foreign tables in psql, run \dE

Ian Hunter
  • 9,466
  • 12
  • 61
  • 77
0

In Postgres, a quick and easy way to show tables is by using the follwoing command.

    /dt

This is a meta-command that is exclusive to the psql interactive terminal for PostgreSQL. A list of all tables in the current database, along with their schema names and table kinds will be shown when you perform the psql command dt.

Moreover, To retrieve a list of the tables in the current database, execute the SQL query below:

    SELECT table
    FROM schema.tables
    WHERE table_schema= 'public';

Hope, this is helpful. Thank you.

Raja Rakshak
  • 168
  • 2
0

This will show all tables in the schema. Run this line of code.

\dt
Aadil Bashir
  • 111
  • 5
-1

First you can connect with your postgres database using the postgre.app on mac or using postico. Run the following command:

psql -h localhost -p port_number -d database_name -U user_name -W

then you enter your password, this should give access to your database

Patcho
  • 77
  • 8