【问题标题】:PostgreSQL psql terminal commandPostgreSQL psql 终端命令
【发布时间】:2012-02-17 21:25:08
【问题描述】:

我正在尝试让psql 格式化并遵循文档here。现在,每当我对有很多列的表进行查询时,无论我的屏幕有多大,每一行都会溢出到下一行,并产生一整屏不可读的垃圾。

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

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

psql

然后按Enter。现在我正在尝试:

psql \pset 格式对齐

得到一个错误:

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

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

【问题讨论】:

  • 在 Ubuntu 13/10 上: sudo -u postgres psql 。然后交互式 shell 中的 \c 会导致“您现在以用户“postgres”的身份连接到数据库“postgres”。

标签: linux postgresql command-line psql


【解决方案1】:

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

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

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

【讨论】:

  • 注意关键字的排列,尽量按相同的顺序写就行了:) - 谢谢亲爱的
  • 安装 PostgreSQL 9.6.10 时,我必须使用psql -h host -p port -d database -U user -W,设置区分大小写。
【解决方案2】:

使用\x postgres 手册中的示例:

    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 + $1 WHERE bid = $2;
    calls      | 3000
    total_time | 20.716706
    rows       | 3000
    -[ RECORD 2 ]------------------------------------------------------------
    userid     | 10
    dbid       | 63781
    query      | UPDATE tellers SET tbalance = tbalance + $1 WHERE tid = $2;
    calls      | 3000
    total_time | 17.1107649999999
    rows       | 3000
    -[ RECORD 3 ]------------------------------------------------------------
    userid     | 10
    dbid       | 63781
    query      | UPDATE accounts SET abalance = abalance + $1 WHERE aid = $2;
    calls      | 3000
    total_time | 0.645601
    rows       | 3000

【讨论】:

    【解决方案3】:
    psql --pset=format=FORMAT
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      相关资源
      最近更新 更多