List the Databases on a PostgreSQL/PgSQL Server
From within the psql monitor, use the following command to list all of the databases in the server:
\l
Alternatively, you can list the databases from the command line using the command as follows:
psql -l








roger pack said on August 6, 2009
worked like a charm
Anonymous said on March 13, 2010
salamo alikom
can you provide it in sql query .
Rob said on April 29, 2010
select d.datname as name from pg_catalog.pg_database d;
Mars99 said on October 14, 2010
I assume you normally want to see a list of all databases NOT including the default PostgreSQL databases. For that you can execute the following SQL query:
SELECT datname FROM pg_database WHERE datistemplate IS FALSE AND datallowconn IS TRUE AND datname!=’postgres’;
To use that from the command line (for scripts…e.g. a backup script) you might want to call it using ‘psql’:
psql –quiet –no-align –tuples-only –dbname=postgres –username=postgres –host=127.0.0.1 –port=5432 –command=”SELECT datname FROM pg_database WHERE datistemplate IS FALSE AND datallowconn IS TRUE AND datname!=’postgres’;”
If you don’t want to be asked by psql for the PostgreSQL password (because you run it in batch script), you need to set the following environment variable in your batch script:
SET PGPASSWORD=yoursecretpassword