帶有 -w 標誌的 pg_dump 不會從 .pgpass 檔案中讀取

帶有 -w 標誌的 pg_dump 不會從 .pgpass 檔案中讀取

我正在嘗試備份名為的 PostgreSQL 資料庫船員資料庫在 Ubuntu 18.04 LTS 上,透過包含以下命令的腳本:

pg_dump -h localhost -p 5432 -U postgres -w -C -F p -b -v -f ~/Dropbox\/postgres_backup/crewdb.backup.sql crewdb

我知道上面命令運行的腳本本身可以工作。當我使用 -W 而不是 -w 運行上述命令時,系統會提示我輸入密碼,並且備份順利進行。我試圖在腳本中自動執行此命令,並希望備份繼續進行,而不提示輸入密碼,因此使用 -w 標誌。為此我創建了以下文件

/home/chh1/.pgpass

什麼時候ls -la ~/.pgpass

-rw------- 1 chh1 chh1 74 Oct 15 10:00 .pgpass

在文件 .pgpass 中我放置了以下文字:

# Server:Port:Database:Username:Password

*:*:crewdb:postgres:9Gh#$mq

但是,當我運行命令時,我收到以下錯誤輸出並且備份失敗:

pg_dump -h localhost -p 5432 -U postgres -w -C -F p -b -v -f ~/Dropbox\/postgres_backup/crewdb.backup.sql crewdb

pg_dump: [archiver (db)] connection to database "crewdb" failed: FATAL: password authentication failed for user "postgres" password retrieved from file "/home/chh1/.pgpass" FATAL: password authentication failed for user "postgres" password retrieved from file "/home/chh1/.pgpass"

如果這裡有人能讓我走上正確的道路,我將非常感激!

答案1

這個問題的答案已經找到了。

跑步時

hexdump -C ~/.pgpass

我得到以下輸出:

00000000  23 20 68 6f 73 74 6e 61  6d 65 3a 70 6f 72 74 3a  |# hostname:port:|
00000010  64 61 74 61 62 61 73 65  3a 75 73 65 72 6e 61 6d  |database:usernam|
00000020  65 3a 70 61 73 73 77 6f  72 64 0a 2a 3a 2a 3a 63  |e:password.*:*:c|
00000030  72 65 77 64 62 3a 70 6f  73 74 67 72 65 73 3a 39  |rewdb:postgres:9|
00000040  47 68 23 24 6d 71 20 0a                           |Gh#$mq .|
00000048

第五行列出了一個 20,它指向密碼末尾但在該行末尾之前的一個空格,表明密碼末尾有一個不應該存在的空格。刪除空格後,指令執行時沒有任何密碼提示。

這個問題的答案由 ServerVault 的 Daniel Vérité 提供。

相關內容