List the databases in a PostgreSQL instance
Published on 30 July 2019
I don’t feel like installinghttps://www.pgadmin.org/[pgAdmin] ou phppgadminwhile everything is available via the command line. + To do this, we must launch the terminal and log in as the postgres user. + The postgres user is the only one who by default has the right to launch the psql application, which is the command-line client (CLI) postgresql, + to be able to communicate with the database via the command line from the terminal. + To connect, open the terminal and type the following:
$ sudo -i -u postgres
To connect and go directly to the PostgreSQL command prompt, type the following:
$ sudo -i -u postgres psql
To list the databases of a PostgreSQL instance, here is the command:
postgres=# \l
result:
List of databases Name | Owner | Encoding | Collation | Character type | Access privileges
fiber | tech | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =Tc/tech + | | | | | tech=CTc/tech fiberdev | tech | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =Tc/tech + | | | | | tech=CTc/tech fiberweb | tech | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =Tc/tech + | | | | | tech=CTc/tech postgres | postgres | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | template0 | postgres | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | fr_FR.UTF-8 | fr_FR.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (6 lignes)
Press q to exit the results display; type \dn to list the schemas of a PostgreSQL instance.
postgres=# SELECT schema_name FROM information_schema.schemata;
result
public information_schema pg_catalog pg_toast_temp_1 pg_temp_1 pg_toast (6 rows)
ou
postgres=# SELECT nspname FROM pg_catalog.pg_namespace;
result
pg_toast pg_temp_1 pg_toast_temp_1 pg_catalog information_schema public (6 rows)