2

I am trying to create bash and sql files to create a user and a database..

Here is what I have done so far..

Setup_postgres.sql

Create USER dummbyuser;
 ALTER USER dummbyuser with superuser;
 ALTER USER dummbyuser with LOGIN;
/q  

Setup_postgres.sh

sudo su - postgress
psql -f setup_postgres.sql;
sh setup_postgres.sql
logout

My problem [SOLVED]

I tell bash to run the Setup_postgres.sh however it hangs after the
sudo su - postgres line The setup_postgres.sql does not run at all.

Any one have any ideas on what I am doing wrong ?
Is this the right approach ?
I have very little experience writing bash files.

Is it possible for the script prompt users for the name of "dummbyuser"

2 Answers 2

3

You are starting a new login shell for the postgres user using sudo su - postgres. After you exit from that shell the rest of your script will run, but not with postgres user privileges.

You could modify your script like this to get it working:

#!/bin/bash

sudo -iu postgres psql < setup_postgres.sql

This will run the specified psql command with postgres user privileges.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you!!! Out of curoisity what does the #!/bin/bash mean (I know it s a comment however I have seen that around) ...Also does -iu do ?...Thanks again!
The #!/bin/bash means that when you run the script bash should be used as interpreter for that script. -iu are sudo options. -i tells sudo to start login shell and -u tells it to run as specified user. You can read more details in sudo help (just type man sudo in the terminal).
0

Also you don't need to sudo over to psql you can just run psql -U psql < setup_postgresql.sql as the default is to trust the username passed. If not is passed it will use the account name it is running under.

Or you can do something like this.

#!/bin/bash
createuser -U psql -s dummbyuser 
createdb -U psql -O dummbyuser dummbyuser

or even using a shell here doc

psql -U psql <<EOF 
 Create USER dummbyuser;
 ALTER USER dummbyuser with superuser;
 ALTER USER dummbyuser with LOGIN;
 EOF  

---answering question from comments---

you can use positional arguments to such as $1 and $2

so for example if your script was called build_sql.sh you could right it like this

#!/bin/bash
createuser -U psql -s $1 
createdb -U psql -O $1 $2 

and then call it like ./build_sql.sh dummbyuser dummbybd

which would replace $1 with dummbyuser and $2 with dummbydb

or you can prompt the user for input and capture it using the read command

so the same build_sql.sh script could become

#!/bin/bash
echo "please enter username followed by [enter]" 
read user 
echo "please enter db_name followed by [enter]" 
read dbname 
createuser -U psql -s $user 
createdb -U psql -O $user $dbname  

then if you want to be really fancy you can combine them

#!/bin/bash
if [ "$1" != "" ]; then
      user=$1
else
  echo "please enter username followed by [enter]"
  read user
fi
if [ "$2" != "" ]; then
  dbname=$2
else
  echo "please enter db_name followed by [enter]"
  read dbname
fi

 createuser -U psql -s $user 
 createdb -U psql -O $user $dbname  

 echo "created new user $user, and gave them a db called $dbname"

1 Comment

Awesome Thankyou! I got it to run perfectly. One question I have for you is do you know how to ask for user input for the "dummbyuser" ?....My group is going to run this script and I would like them to enter there user name without editing the file. Thanks Again!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.