执行psql命令的Shell脚本

执行psql命令的Shell脚本

我想制作一个自动化脚本来在 Postgresql 中创建数据库用户和密码,并导入一些数据库。当我执行下面的脚本时,它会停止在某处,当我注销(CTRL+D 或退出命令)时,它会尝试导入数据库,并显示:

psql: FATAL:  role "username" does not exist

最后它不会转到 /tmp 我使用的是 Ubuntu 14.10,这是我的脚本:

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

当我执行脚本时

答案1

问题是退出命令。我把它放在脚本的末尾。它工作得很好。这是我的脚本:

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

答案2

我写的回复太长,无法发表评论。

添加调试信息不​​会修复脚本,但是,它将帮助您找到脚本的哪一部分出现故障。在数据库部分,您需要在每个sql脚本之前添加echo:

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

这不会修复你的脚本。但是,您会知道哪个 SQL 脚本挂起。然后,我或另一个 SE 可以(接下来)帮助您调试 sql 脚本,然后(最后)修复安装。如果脚本通过了所有 sql 脚本(使其到达输出Finished Database section),我们将尝试调试脚本的其余部分,但让我们首先查看 sql 输入文件。

相关内容