Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of database names using psql

I'm trying to get a list of database names using the psql command. So far I have:

psql -h example.com -U backup -c '\l'

This however gives me the results in a table-like format. I ONLY want the table names (one on each line). How would I accomplish this?

like image 745
Kyle Decot Avatar asked Sep 09 '14 14:09

Kyle Decot


People also ask

How do I list all databases and tables using psql?

Summary. Use \l or \l+ in psql to show all databases in the current PostgreSQL server. Use the SELECT statement to query data from the pg_database to get all databases.

What is the psql command to show the schema?

To show tables of all schemas use \dt *. * and for a particular schema use \dt schema_name.

What are psql meta commands?

Meta-Commands. Anything you enter in psql that begins with an unquoted backslash is a psql meta-command that is processed by psql itself. These commands make psql more useful for administration or scripting. Meta-commands are often called slash or backslash commands.

How do I get a list of column names in PostgreSQL?

To list down all tables columns on a specific table in the a PostgreSQL database using psql command-line, you can use \dS your_table_name.


2 Answers

This does it:

psql -h example.com -U backup -t -A -c "SELECT datname FROM pg_database WHERE datname <> ALL ('{template0,template1,postgres}')"

Using the system catalog pg_database.

Read the manual about psql.

like image 66
Erwin Brandstetter Avatar answered Sep 28 '22 06:09

Erwin Brandstetter


I don't think you can use \l for that.

But the following should do it:

psql -h example.com -U backup -t -c "select datname from pg_database"

\t turns off the header and the other "noise". And the select statement only returns the database name.

Not sure if you need to use single quotes or double quotes for the SQL statement - on my Windows console I had to use double quotes.

like image 34
a_horse_with_no_name Avatar answered Sep 28 '22 05:09

a_horse_with_no_name