What\'s the equivalent to show tables
(from MySQL) in PostgreSQL?
First login as postgres user:
sudo su - postgres
connect to the required db: psql -d databaseName
\dt
would return the list of all table in the database you're connected to.
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.
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
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.
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]"
Type the following command to list all the tables.
\dt
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.