0

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

when i execute the script

closed as off-topic by John WH Smith, Archemar, jasonwryan, slm Mar 24 '15 at 21:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – John WH Smith, Archemar, jasonwryan, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You will need to debug it. It sounds like one of the sql scripts you are running doesn't work right. In your script, try to print (echo) the file name to your screen to see which is hanging. For instance, insert the line echo "Running postgis.sql" just before psql -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:43
  • Thank you @Andrew it fails anyway. I added the link to my script at the and of the question. Thank you very much – Koçer Mar 23 '15 at 15:03
  • As for "At the end it doens't go to /tmp"--even if it does, it won't have any effect on the shell you run the script from, unless you source the script in instead of executing it. – PSkocik Mar 23 '15 at 15:19
  • @PSkocik i didn't understand what you mean – Koçer Mar 23 '15 at 15:21
  • Assuming script.sh is the name of your script, if you run ./script.sh, and cding done in script.sh won't have any effect on the shell you start script.sh from. If you do source ./script.sh, it'll be as if you pasted the contents of script.sh into your current shell session and cds will have an effect. – PSkocik Mar 23 '15 at 15:29
4

The problem was the exit command. i placed it at the end of my script. it works perfectly kow. Here is my script:

#!/bin/bash
#################
# Change this values
#################


psqluser="koko28"   # Database username
psqlpass="pass123"  # Database password
psqldb="kokodb28"   # Database name


#################################################
#                       #
#    PLEASE DO NOT CHANGE THE FOLLOWING CODES   #
#                       #
#################################################

#################
# Dependicies
#################
sudo apt-get update
sudo apt-get update
sudo apt-get install unzip apache2 php5 php5-gd php5-curl php5-pgsql postgresql-9.4 postgis postgresql-9.4-postgis-2.1 postgresql-contrib-9.4 tomcat8 git g++ php5-dev libgdal1-dev drush -y


#################
# Database
#################
sudo printf "CREATE USER $psqluser WITH PASSWORD '$psqlpass';\nCREATE DATABASE $psqldb WITH OWNER $psqluser;" > cartaro.sql

sudo -u postgres psql -f cartaro.sql

echo "Running postgis.sql"
sudo -u postgres psql -d $psqldb -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis.sql

echo "Running postgis_comments.sql"
sudo -u postgres psql -d $psqldb -f /usr/share/postgresql/9.4/contrib/postgis-2.1/postgis_comments.sql

echo "Running spatial_ref_sys.sql"
sudo -u postgres psql -d "$psqldb" -f /usr/share/postgresql/9.4/contrib/postgis-2.1/spatial_ref_sys.sql

sudo -u postgres psql -d "$psqldb" -c "grant all on geometry_columns to '$psqluser';"
sudo -u postgres psql -d "$psqldb" -c "grant all on spatial_ref_sys to '$psqluser';"

echo "Finished Database section"

#################
# Tomcat configuration
#################
cd /tmp
echo "Backing  up tomcat web.xml origin file"
sudo mv  /var/lib/tomcat8/conf/web.xml /var/lib/tomcat8/conf/web.bak
echo "Getting custom one"
wget http://qgis.fr/script/tomcat.xml
echo "Replacing"
sudo mv tomcat.xml /var/lib/tomcat8/conf/web.xml
echo "Tomcat configration finished"

#################
# Geoserver
#################
echo "Getting goserverip file"
wget http://qgis.fr/script/geoserver.zip
echo "Putting in the riht places"
sudo unzip -o geoserver.zip geoserver.war -d /var/lib/tomcat8/webapps/
echo "Backing up geoserver web.xml origin file"
sudo mv  /var/lib/tomcat8/webapps/geoserver/WEB-INF/web.xml /var/lib/tomcat8/webapps/geoserver/WEB-INF/web.bak
echo "Getting custom one"
wget http://qgis.fr/script/geoserver.xml
echo "Replacing"
sudo mv geoserver.xml /var/lib/tomcat8/conf/web.xml
echo "Restart tomcat service to take effect the changes"
service tomcat8 restart
echo "Geoserver configration finished"



#################
# Gdal
#################
git clone https://github.com/geonef/php5-gdal.git && cd php5-gdal
phpize
./configure
make
sudo make install
printf '; configuration for GDAL module\n extension=gdal.so\n' > /etc/php5/mods-available/gdal.ini
php5enmod gdal
sudo service apache2 restart

#################
# Preparing Cartaro
#################
echo "Getting goserverip file"
wget http://qgis.fr/script/cartaro.zip
echo "Putting in the riht places"
sudo unzip -o cartaro.zip
sudo cp -r cartaro/* /var/www/html/
sudo cp /var/www/html/sites/default/default.settings.php /var/www/html/sites/default/settings.php
sudo mkdir /var/www/html/sites/default/files/
sudo chmod -R 777 /var/www/html/sites/default/*
sudo rm /var/www/html/index.html


#################
# Cartaro
#################
cd /var/www/html/
echo "Installing Cartaro"
sudo drush site-install cartaro --db-url="pgsql://$psqluser:$psqlpass@localhost/$psqldb" --site-name=Koko site  --account-name=admin --site-mail=evdalo@gmail.com --account-pass=geoserver --yes


#################
# Cleaning up
#################
echo "Cleaning"
sudo rm -r /tmp/cartaro* /tmp/geo* 

echo "Cleaned"

echo "End of the script"

exit
  • I don't understand, your exit was always at the end of your script. If you have found the mistake, please show us the entire, correct script and then accept your answer. – terdon Mar 24 '15 at 15:56
  • Look at the end of my question. You will see the link of my last script – Koçer Mar 24 '15 at 16:28
  • 3
    OK, but please include all necessary information in your question. Don't expect people to download random files in order to help you. As it stands, this is not actually answering the question you asked. Why don't you just include the corrected script here so the question can be marked as answered? – terdon Mar 24 '15 at 16:30
  • @terdon thank you man. just edited the answer :) – Koçer Mar 24 '15 at 20:12
  • @Kocer, good job. – Andrew Mar 24 '15 at 21:58
0

The response I was writing is to is too long for a comment.

Adding the debugging info won't fix the script, however, it will help you find which part of the script is failing. In the Database section, you need to add echo before each sql script:

#################
# Database
#################

sudo su postgres
wget http://qgis.fr/script/cartaro.sql
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

This won't fix your script. But, you will know which SQL script is hanging. Then, I or another SE can (next) help you debug the sql script, then (finally) fix the install. If the script makes it past all of the sql scripts (makes it to the Finished Database section output), we'll look to debug the rest of the script, but let's look at that sql input files first.

  • It stops again with no error. – Koçer Mar 23 '15 at 16:12
  • I think the problem is that it stays at postgres@localhost it doesn't log out the postgres user – Koçer Mar 23 '15 at 16:15
  • The "exit" after echo "Finished Database section" is the exit for the whole script or for logout the postgres user?? – Koçer Mar 23 '15 at 16:17
  • Just edited my .sh file in the first post. Thank you to take the time to look – Koçer Mar 23 '15 at 18:28

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