Linux PostgreSQL psql 终端命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8990098/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:12:27  来源:igfitidea点击:

PostgreSQL psql terminal command

linuxpostgresqlcommand-linepsql

提问by IAmYourFaja

I'm trying to get psqlto format nicely and am following the docs here. Right now, whenever I do a query on tables with lots of columns, no matter how big I make my screen each line overflows into the next line and producing a whole screen of unreadable junk.

我正在尝试psql很好地格式化,并且正在关注此处的文档。现在,每当我对包含大量列的表进行查询时,无论我将屏幕设置为多大,每一行都会溢出到下一行并产生一整屏不可读的垃圾。

The docs (link is above) say there's a way to align columns nicely for more readable output.

文档(链接在上面)说有一种方法可以很好地对齐列以获得更具可读性的输出。

Normally, to start psql, I just type:

通常,要开始psql,我只需键入:

psql

查询语句

and hit Enter. Now I'm trying:

并按Enter。现在我正在尝试:

psql \pset format aligned

psql \pset 格式对齐

And getting an error:

并得到一个错误:

could not change directory to "/root"
psql: warning: extra command-line argument "aligned" ingored
psql: FATAL: Indent authentication failed for user "format"

Any ideas as to how I could get these command-line args to work for me?

关于如何让这些命令行参数为我工作的任何想法?

采纳答案by Szymon Lipiński

These are not command line args. Run psql. Manage to log into database (so pass the hostname, port, user and database if needed). And then write it in the psql program.

这些不是命令行参数。运行 psql。管理登录数据库(如果需要,请传递主机名、端口、用户和数据库)。然后写在p​​sql程序中。

Example (below are two commands, write the first one, press enter, wait for psql to login, write the second):

例子(下面是两条命令,写第一个,回车,等待psql登录,写第二个):

psql -h host -p 5900 -U username database
\pset format aligned

回答by n0nSmoker

Use \xExample from postgres manual:

使用\xpostgres 手册中的示例:

    postgres=# \x
    postgres=# SELECT * FROM pg_stat_statements ORDER BY total_time DESC LIMIT 3;
    -[ RECORD 1 ]------------------------------------------------------------
    userid     | 10
    dbid       | 63781
    query      | UPDATE branches SET bbalance = bbalance +  WHERE bid = ;
    calls      | 3000
    total_time | 20.716706
    rows       | 3000
    -[ RECORD 2 ]------------------------------------------------------------
    userid     | 10
    dbid       | 63781
    query      | UPDATE tellers SET tbalance = tbalance +  WHERE tid = ;
    calls      | 3000
    total_time | 17.1107649999999
    rows       | 3000
    -[ RECORD 3 ]------------------------------------------------------------
    userid     | 10
    dbid       | 63781
    query      | UPDATE accounts SET abalance = abalance +  WHERE aid = ;
    calls      | 3000
    total_time | 0.645601
    rows       | 3000

回答by Nowaker

psql --pset=format=FORMAT

Great for executing queries from command line, e.g.

非常适合从命令行执行查询,例如

psql --pset=format=unaligned -c "select bandanavalue from bandana where bandanakey = 'atlassian.confluence.settings';"