Script shell para executar o comando psql [closed]

0

Eu quero fazer um script automatizado para criar um usuário e uma senha de banco de dados no Postgresql e também importar alguns bancos de dados. Quando eu executo o meu script abaixo ele pára em algum lugar e quando eu sair (CTRL + D ou comando de saída) ele tenta importar banco de dados e diz:

psql: FATAL:  role "username" does not exist

No final, não vai para / tmp Estou usando o Ubuntu 14.10 e aqui está o meu 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

    
por Koçer 23.03.2015 / 15:01

2 respostas

3

O problema foi o comando de saída. Eu coloquei no final do meu script. Funciona perfeitamente. Aqui está o meu 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 [email protected] --account-pass=geoserver --yes


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

echo "Cleaned"

echo "End of the script"

exit
    
por 24.03.2015 / 16:30
0

A resposta que eu estava escrevendo é muito longa para um comentário.

Adicionar as informações de depuração não corrigirá o script, no entanto, ele ajudará a descobrir qual parte do script está falhando. Na seção Database, você precisa adicionar echo antes de cada script sql:

#################
# 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

Isso não consertará seu script. Mas você saberá qual script SQL está pendente. Então, eu ou outro SE pode (próximo) ajudá-lo a depurar o script sql, então (finalmente) corrigir a instalação. Se o script ultrapassar todos os scripts sql (chegar à Finished Database section output), procuraremos depurar o restante do script, mas vamos ver primeiro os arquivos de entrada sql.

    
por 23.03.2015 / 16:29