How do I list all databases and tables using psql?
Please note the following commands:
\list
or\l
: list all databases\dt
: list all tables in the current database using yoursearch_path
\dt *.
: list all tables in the current database regardless yoursearch_path
You will never see tables in other databases, these tables aren't visible. You have to connect to the correct database to see its tables (and other objects).
To switch databases:
\connect database_name
or \c database_name
See the manual about psql.
This lists databases:
SELECT datname FROM pg_database
WHERE datistemplate = false;
This lists tables in the current database
SELECT table_schema,table_name
FROM information_schema.tables
ORDER BY table_schema,table_name;
In Postgresql these terminal commands list the databases available
el@defiant$ /bin/psql -h localhost --username=pgadmin --list
Or the command stated more simply:
psql -U pgadmin -l
Those commands print this on the terminal:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
kurz_prod | pgadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
pgadmin | pgadmin | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(5 rows)
These are the available databases.
In PSQL these commands list the tables available
You have to specify a database before you can list the tables in that database.
el@defiant$ psql -U pgadmin -d kurz_prod
This brings you to a psql terminal:
kurz_prod=#
Use the command \d
meaning show all tables, views, and sequences
kurz_prod=# \d
This prints:
List of relations
Schema | Name | Type | Owner
--------+---------+----------+---------
public | mytable | table | pgadmin
public | testing | sequence | pgadmin
(2 rows)
Then, to exit the psql terminal, type \q
and press enter. Or Ctrl-D
does the same thing. These are the tables in that database.