Fixed errors with PostgreSQL when DB (not) exists

This commit is contained in:
Alexey Pustovalov 2020-08-04 14:20:18 -04:00
commit cb4faf5a6f
3 changed files with 18 additions and 6 deletions

View File

@ -196,7 +196,11 @@ check_db_connect_postgresql() {
export PGSSLKEY=${ZBX_DBTLSKEYFILE} export PGSSLKEY=${ZBX_DBTLSKEYFILE}
fi fi
while [ ! "$(pg_isready --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --dbname ${DB_SERVER_DBNAME} --quiet 2>/dev/null && echo $?)" ]; do while true :
do
psql --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --list --quiet 2>&1 1>/dev/null && break
psql --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --list --dbname ${DB_SERVER_DBNAME} --quiet 2>&1 1>/dev/null && break
echo "**** PostgreSQL server is not available. Waiting $WAIT_TIMEOUT seconds..." echo "**** PostgreSQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
sleep $WAIT_TIMEOUT sleep $WAIT_TIMEOUT
done done
@ -259,7 +263,7 @@ create_db_user_postgresql() {
} }
create_db_database_postgresql() { create_db_database_postgresql() {
DB_EXISTS=$(psql_query "SELECT 1 AS result FROM pg_database WHERE datname='${DB_SERVER_DBNAME}'") DB_EXISTS=$(psql_query "SELECT 1 AS result FROM pg_database WHERE datname='${DB_SERVER_DBNAME}'" "${DB_SERVER_DBNAME}")
if [ -z ${DB_EXISTS} ]; then if [ -z ${DB_EXISTS} ]; then
echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..." echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..."

View File

@ -196,7 +196,11 @@ check_db_connect_postgresql() {
export PGSSLKEY=${ZBX_DBTLSKEYFILE} export PGSSLKEY=${ZBX_DBTLSKEYFILE}
fi fi
while [ ! "$(pg_isready --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --dbname ${DB_SERVER_DBNAME} --quiet 2>/dev/null && echo $?)" ]; do while true :
do
psql --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --list --quiet 2>&1 1>/dev/null && break
psql --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --list --dbname ${DB_SERVER_DBNAME} --quiet 2>&1 1>/dev/null && break
echo "**** PostgreSQL server is not available. Waiting $WAIT_TIMEOUT seconds..." echo "**** PostgreSQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
sleep $WAIT_TIMEOUT sleep $WAIT_TIMEOUT
done done
@ -259,7 +263,7 @@ create_db_user_postgresql() {
} }
create_db_database_postgresql() { create_db_database_postgresql() {
DB_EXISTS=$(psql_query "SELECT 1 AS result FROM pg_database WHERE datname='${DB_SERVER_DBNAME}'") DB_EXISTS=$(psql_query "SELECT 1 AS result FROM pg_database WHERE datname='${DB_SERVER_DBNAME}'" "${DB_SERVER_DBNAME}")
if [ -z ${DB_EXISTS} ]; then if [ -z ${DB_EXISTS} ]; then
echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..." echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..."

View File

@ -196,7 +196,11 @@ check_db_connect_postgresql() {
export PGSSLKEY=${ZBX_DBTLSKEYFILE} export PGSSLKEY=${ZBX_DBTLSKEYFILE}
fi fi
while [ ! "$(pg_isready --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --dbname ${DB_SERVER_DBNAME} --quiet 2>/dev/null && echo $?)" ]; do while true :
do
psql --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --list --quiet 2>&1 1>/dev/null && break
psql --host ${DB_SERVER_HOST} --port ${DB_SERVER_PORT} --username ${DB_SERVER_ROOT_USER} --list --dbname ${DB_SERVER_DBNAME} --quiet 2>&1 1>/dev/null && break
echo "**** PostgreSQL server is not available. Waiting $WAIT_TIMEOUT seconds..." echo "**** PostgreSQL server is not available. Waiting $WAIT_TIMEOUT seconds..."
sleep $WAIT_TIMEOUT sleep $WAIT_TIMEOUT
done done
@ -259,7 +263,7 @@ create_db_user_postgresql() {
} }
create_db_database_postgresql() { create_db_database_postgresql() {
DB_EXISTS=$(psql_query "SELECT 1 AS result FROM pg_database WHERE datname='${DB_SERVER_DBNAME}'") DB_EXISTS=$(psql_query "SELECT 1 AS result FROM pg_database WHERE datname='${DB_SERVER_DBNAME}'" "${DB_SERVER_DBNAME}")
if [ -z ${DB_EXISTS} ]; then if [ -z ${DB_EXISTS} ]; then
echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..." echo "** Database '${DB_SERVER_DBNAME}' does not exist. Creating..."