3

I have connected a database using the command

sudo -i -u postgres

And after I have tried \l and \d too, to view the list of all available databases, it is not working. Instead, I get the following output:

d:command not found and 
l:command not found error is showing 

What to do, how to get the list of the postgresql databases?

asked Nov 27, 2017 at 5:52
1
  • 1
    You need to run the command line tool psql. sudo itself just changes your current user Commented Nov 27, 2017 at 6:46

5 Answers 5

-1

You will have to log in to the database using the psql (see below) command line tool (or use pgadmin or similar tool). The error you are getting appears to be a bash (i.e. Linux shell) error.

This information you want is available through SQL. You can obtain it in 1 of two ways:

SELECT nspname
FROM pg_catalog.pg_namespace;

Or, you can use the standard ANSI method,

SELECT schema_name
FROM information_schema.schemata;

You can find out more about this and more in the PostgreSQL system catalogs documentation here.

Using psql (command to start this is psql [db_name] from the shell), you can also use \dn and/or \l (as explained by @a_horse_with_no_name) if you prefer psql tool commands!

In bash, the \ character is used:

  • to disable aliases at the beginning of a command (explained here), or

  • as an escape character (within a command - explained in bash ref 2) or

  • to allow a command to span multiple lines (at the end of a line on its own - also explained in bash ref 2).

Try running:

ls \
-lista 

from the shell (cut and paste as-is). After it has run (returns the long listing of your current directory), hit the up-arrow to see what's happening! The ls -lista is expanded on a single line, however, it can be great for helping make long commands legible!

answered Nov 27, 2017 at 6:36
4
  • The information is available in psql, \l and \d will do that. But apparently Abhinaw is not executing those commands in psql but on the linux command line Commented Nov 27, 2017 at 6:48
  • thanks @a_horse_with_no_name i got solution is 'psql \l' Commented Nov 27, 2017 at 6:56
  • \dn shows schemata. \l shows databases. Commented Nov 27, 2017 at 10:05
  • The question is how to list databases. This shows schemas SELECT datname FROM pg_database; Commented Sep 30, 2023 at 13:56
7

If you want to execute the command from the shell, you can use the -c option in psql:

psql -c '\l'

Example:

peter@blackrain:~$ sudo su postgres
postgres@blackrain:/home/peter$ psql -c '\l'
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
------------+----------+----------+-------------+-------------+-----------------------
 blackrain | peter | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | 
 postgres | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | 
 redpepper | peter | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | 
 template0 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
 template1 | postgres | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
 x | peter | UTF8 | en_GB.UTF-8 | en_GB.UTF-8 | 
(6 rows)
postgres@blackrain:/home/peter$
answered Nov 27, 2017 at 10:00
3

If you only want to see the list of databases and have psql available to you, you can simply use -l to list and exit.

psql -l

https://www.postgresql.org/docs/current/app-psql.html

answered Sep 13, 2022 at 16:46
0

This lists all databases:

SELECT datname FROM pg_database;

These list all databases in detail:

\list
\l

These list all databases in more detail:

\list+
\l+
answered Sep 23, 2023 at 0:31
-1

For getting the database names only:

echo "SELECT datname FROM pg_database;" | psql | tail -n +3 | head -n -2 | egrep -v 'template0|template1|postgres'
Laurenz Albe
62.1k4 gold badges57 silver badges93 bronze badges
answered Jun 25, 2020 at 22:12
1
  • I believe it is an answer in its current form, but a snippet-only one. Commented Jun 26, 2020 at 19:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.