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?
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.
To show tables of all schemas use \dt *. * and for a particular schema use \dt schema_name.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With