Score:0

Ubuntu shell script for starting multiple Postgres servers

ru flag

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

Score:2
it flag

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.

Shafin M avatar
ru flag
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 avatar
ru flag
Looks like its looking for more arguments? Not sure what else I'm supposed to pass in here
hr flag
@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
waltinator avatar
it flag
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.
Shafin M avatar
ru flag
@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
hr flag
@ShafinM where is it located though? remember `sudo` uses its own `secure_path` - which may be different from your interactive shell's `PATH`. If in doubt, use the absolute path ex. `sudo -u postgres /path/to/postgres <other args>`
Score:0
ru flag

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 &
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.