I want to make an automated script to make a database user and password in Postgresql and also import some databases. When i execute my script bellow it stops somewhere and when i log out (CTRL+D or exit command) it tries to import database and it says:
psql: FATAL: role "username" does not exist
At the end it doens't go to /tmp I'm using Ubuntu 14.10 and here is my script:
#!/bin/bash -x
#################
# Database
#################
printf 'CREATE USER koko WITH NOCREATEDB NOCREATEROLE NOSUPERUSER ENCRYPTED PASSWORD 'kokopass';\nCREATE DATABASE kokodb WITH OWNER koko;' > cartaro.sql
su postgres
psql -f cartaro.sql
echo "Running postgis.sql"
psql -d "kokodb" -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis.sql
echo "Running postgis_comments.sql"
psql -d "kokodb" -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis_comments.sql
echo "Running spatial_ref_sys.sql"
psql -d "kokodb" -f /usr/share/postgresql/9.4/contrib/postgis-2.1/spatial_ref_sys.sql
psql -d "kokodb" -c 'grant all on geometry_columns to "koko";'
psql -d "kokodb" -c 'grant all on spatial_ref_sys to "koko";'
echo "Finished Database section"
exit
echo "Running postgis.sql"
just beforepsql -d "kokodb" -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis.sql
and so on. This will help you determine (at least) which of the sql scripts is not finishing. You can also set your first line to#!/bin/bash -x
to get a little more output. – Andrew Mar 23 '15 at 14:43script.sh
is the name of your script, if you run./script.sh
, andcd
ing done inscript.sh
won't have any effect on the shell you startscript.sh
from. If you dosource ./script.sh
, it'll be as if you pasted the contents ofscript.sh
into your current shell session andcd
s will have an effect. – PSkocik Mar 23 '15 at 15:29