使用遠端 ssh 更改 postgres 密碼

使用遠端 ssh 更改 postgres 密碼

我在 Google Cloud 實例上執行一個 postgres 資料庫,作為每晚建置的一部分,我們每天都會刪除並重新建立該資料庫。目前這是手動完成的,我想自動化它。

gcloud compute ssh --zone europe-west2-c postgres@postgresql-dev -- "dropdb mydb"
gcloud compute ssh --zone europe-west2-c postgres@postgresql-dev -- "createdb mydb"
gcloud compute ssh --zone europe-west2-c postgres@postgresql-dev -- "psql postgres -c 'GRANT ALL PRIVILEGES ON DATABASE mydb to myuser;' "

這可以很好地刪除並重新建立資料庫,當我必須重新設定密碼時就會出現問題...

gcloud compute ssh --zone europe-west2-c postgres@postgresql-dev -- "psql postgres -c 'ALTER USER myuser WITH PASSWORD 'passwordhere' ; "

gcloud指令已經使用雙引號,psql postgres -c已經使用單引號,我該使用什麼引號將密碼放在引號中?

我嘗試轉義引號,但它不起作用:

WITH PASSWORD \'passwordhere\' 

錯誤:「\」第 1 行或附近的語法錯誤:ALTER USER myuser WITH PASSWORD \passwordhere'

或者

WITH PASSWORD \"passwordhere\" 

bash: -c: 第 0 行:尋找符合的「」時出現意外的 EOF bash: -c: 第 1 行:語法錯誤:意外的檔案結尾

我該如何擺脫這些引號?

答案1

這似乎有效:

gcloud compute ssh --zone europe-west2-c postgres@postgresql-dev -- "psql postgres -c 'ALTER USER myuser WITH PASSWORD '\''passwordhere'\'' '; "

-

WITH PASSWORD '\''passwordhere'\'' 

相關內容