2012-01-24 216 views
36

我想要psql格式很好,並遵循文檔here。現在,每當我對包含大量列的表執行查詢時,無論我的屏幕有多大,每行都會溢出到下一行,併產生整個屏幕上不可讀的垃圾。PostgreSQL的psql終端命令

文檔(鏈接在上面)說,有一種方法可以很好地對齊列以獲得更多可讀的輸出。

通常情況下,開始psql,我只需要輸入:

psql

,打輸入。現在,我想:

psql \pset format aligned

,卻得到了錯誤:

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

任何想法,我怎麼能拿這些命令行參數來爲我工作?

+2

在Ubuntu 13/10上:sudo -u postgres psql。然後\ c在交互式shell中導致「您現在連接到數據庫」postgres「作爲用戶」postgres「。 – Timo

回答

65

這些不是命令行參數。運行psql。管理登錄到數據庫(如果需要,傳遞主機名,端口,用戶和數據庫)。然後將其寫入psql程序中。

實施例(下面兩個命令,寫第一個,按下輸入鍵,等待PSQL登錄,寫入第二):

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

請注意關鍵字的排列方式,嘗試按照相同的順序編寫它,並且工作:) - 謝謝親愛的 – ImranNaqvi

5

使用從postgres的手動\x 實施例:

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 
4
psql --pset=format=FORMAT 

非常適合從命令行執行查詢,例如

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

步驟從窗口-7的命令行連接到PostgreSQL v9.3版本服務器如下 -

1. Go to the start --> All programs --> postgresql 9.3 --> Sql Shell (psql) 
2. Just enter the details requested by postgres server, I've shown log snippet from my CMD. 

(注: 「postgres的」 用戶將是PostgreSQL服務器超級用戶,密碼應該是你在安裝postgresql時輸入的密碼)

Server [localhost]: localhost 
Database [postgres]: postgres 
Port [5432]: 5432 
Username [postgres]: postgres 
Password for user postgres: 
psql (9.3.5) 
WARNING: Console code page (437) differs from Windows code page (1252) 
     8-bit characters might not work correctly. See psql reference 
     page "Notes for Windows users" for details. 
Type "help" for help. 

postgres=# CREATE ROLE prateek LOGIN PASSWORD 'test_123' NOINHERIT CREATEDB; 
CREATE ROLE 
postgres=#