To list all postgresql databases, we need to first switch to user postgres using the command su psql. Next we need to login to the postgresql console using the command psql. After that run the command \list. This will list all databases. To run queries on the database fist run the command \connect database_name. Then run your query.