The psql --list command can be used to display the Postgres databases. Something like this should be returned.
~]# psql --username johndoe --dbname db001 --list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
db001 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
db002 | john.doe | UTF8 | en_US.utf8 | en_US.utf8 |
The psql command with the \dt (describe table) command can be used to list the tables in a database. If using the default posgres user account, I typically go with the following command.
sudo -u postgres psql --command "\dt"
If you have created a user account, the -u or --username option and --password flag can be used.
psql --username johndoe --password --command "\dt"
Better yet, you can setup passwordless authentication using the hidden .pgpass file and then connect without the --password flag.
psql --username johndoe --dbname mydb --command "\dt"
If did not find any relations is returned, this means the database contains no tables.
~]# psql --username johndoe --dbname mydb --command "\dt"
Did not find any relations.
Something like this should be returned.
List of relations
Schema | Name | Type | Owner
--------+-------------------------------+-------+-------
public | table001 | table | johndoe
public | table002 | table | johndoe
public | table003 | table | johndoe
Or, the -x or --expanded flag can be used for a different format output.
~]$ psql --username johndoe --dbname mydb --expanded --command "\dt"
List of relations
-[ RECORD 1 ]---
Schema | public
Name | mytable
Type | table
Owner | johndoe
Did you find this article helpful?
If so, consider buying me a coffee over at