0

I'm fairly new to Ubuntu and I'm tying to write a .sh script that starts up 2 postgres servers. I disabled pw requirement for sudo in /etc/sudoers

here is my script so far and it's probably incorrect. test.sh:

#!/bin/bash
sudo su postgres
postgres -D /usr/local/mydb1 -p <port no.> &
postgres -D /usr/local/mydb2/data -p <diff port no.> &

The outcome I get after running the script:

shafin@shafin-VirtualBox:~/Desktop$ ./test.sh
postgres@shafin-VirtualBox:/home/shafin/Desktop$ 

Looks like it's only executing the first line of the script.

can anyone please help?

Thanks, Shafin

2

Your sudo su postgres command starts a new shell, under the "postgres" userid. This new shell doesn't read from the script file, so it never sees the rest of the file.

A better way (that works) is to remove the sudo su postgres command, and preface the remaining two lines with sudo -u postgres. Read man sudo sudoers.

6
  • So I tried this line: sudo -u postgres -D /usr/local/capdb -p 8432 & and the response i get is this: usage: sudo -h | -K | -k | -V usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user] usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command] usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>] usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ... – Shafin M Jun 17 at 22:53
  • Looks like its looking for more arguments? Not sure what else I'm supposed to pass in here – Shafin M Jun 17 at 22:55
  • 2
    @ShafinM sudo -u postgres postgres -D /usr/local/capdb -p 8432 & - the first postgres is the user argument to the sudo -u option; the second postgres is the command – steeldriver Jun 17 at 23:45
  • Comments are designed for US to ask YOU questions about your Question. You should Edit your question to add information. By updating your Question, and using the formatting buttons, you make all the information available to new readers. People shouldn't have to read a long series of comments to get the whole story. – waltinator Jun 18 at 2:35
  • @steeldriver already tried that command, bash replies with a unknown command 'postgres' . I know for sure Postgres is set up correctly on this machine because I'm able to start the servers one by one – Shafin M Jun 18 at 16:23
0

Thanks @waltinator and @steeldriver for your feedbacks.

I was able to work out a solution using both of your suggestions. Here's my updated script in case anyone is looking to do something similar:

#!/bin/bash

sudo -u postgres /usr/lib/postgresql/12/bin//postgres -D /usr/local/mydb1 -p 8432 &
sudo -u postgres /usr/lib/postgresql/12/bin//postgres -D /usr/local/mydb2/data -p 6432 &

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.