简体   繁体   中英

how to `sudo su postgres` on a remote server with a script

I am sure this question would have come up thousands of time but I still cant find the answer or perhaps I am too dumb for it. here it is.
I am trying to ssh a server and then take the postgresql database backup. I tried this.

#!/bin/sh
ssh admin@myserver 'NOW=$(date +"%m-%d-%Y"); cd /home/open/backup; sudo su postgres; pg_dump v2 > v2-$NOW.sql;'

The problem think is that when I do sudo su postgres it asks for a password and their it gets stuck. any pointers would be helpful.

You can edit /etc/sudoers to allow the user admin to run commands as postgres without password:

admin ALL=(postgres) NOPASSWD: ALL

You could also narrow it down to specific commands rather than ALL if you prefer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM