원격 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: line 0: 일치하는 `''을 찾는 동안 예상치 못한 EOF bash: -c: line 1: 구문 오류: 예상치 못한 파일 끝

그 따옴표를 어떻게 피합니까?

답변1

이것은 작동하는 것 같습니다 :

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

-

WITH PASSWORD '\''passwordhere'\'' 

관련 정보