bash 使用 psql 获取数据库名称列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25747452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get list of database names using psql
提问by Kyle Decot
I'm trying to get a list of database names using the psql
command. So far I have:
我正在尝试使用该psql
命令获取数据库名称列表。到目前为止,我有:
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?
然而,这给了我类似表格格式的结果。我只想要表名(每行一个)。我将如何做到这一点?
回答by Erwin Brandstetter
This should do it:
这应该这样做:
psql -h example.com -U backup -t -A -c 'SELECT datname FROM pg_database'
Using the system catalog pg_database
.
Details about psql in the manual.
使用系统目录pg_database
。
手册中有关 psql 的详细信息。
回答by a_horse_with_no_name
I don't think you can use \l
for that.
我不认为你可以用\l
它。
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.
\t
关闭标题和其他“噪音”。而 select 语句只返回数据库名称。
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.
不确定您是否需要对 SQL 语句使用单引号或双引号 - 在我的 Windows 控制台上,我不得不使用双引号。