94c654ef37
Now that we have multiple workers running by default in various projects (nova/cinder/glance/trove), the postgresql job is failing intermittently with connection failures to the database. The default max_connections for postgresql is 100 so here we double that. Note that the default max_connections for mysql used to be 100 but is now 151, so this change brings the postgresql configuration more in line with mysql. Change-Id: I2fcae8184a82e303103795a7bf57c723e27190c9 Closes-Bug: #1338841
119 lines
3.6 KiB
Plaintext
119 lines
3.6 KiB
Plaintext
# lib/databases/postgresql
|
|
# Functions to control the configuration and operation of the **PostgreSQL** database backend
|
|
|
|
# Dependencies:
|
|
#
|
|
# - DATABASE_{HOST,USER,PASSWORD} must be defined
|
|
|
|
# Save trace setting
|
|
PG_XTRACE=$(set +o | grep xtrace)
|
|
set +o xtrace
|
|
|
|
|
|
MAX_DB_CONNECTIONS=${MAX_DB_CONNECTIONS:-200}
|
|
|
|
|
|
register_database postgresql
|
|
|
|
|
|
# Functions
|
|
# ---------
|
|
|
|
# Get rid of everything enough to cleanly change database backends
|
|
function cleanup_database_postgresql {
|
|
stop_service postgresql
|
|
if is_ubuntu; then
|
|
# Get ruthless with mysql
|
|
apt_get purge -y postgresql*
|
|
return
|
|
elif is_fedora || is_suse; then
|
|
uninstall_package postgresql-server
|
|
else
|
|
return
|
|
fi
|
|
}
|
|
|
|
function recreate_database_postgresql {
|
|
local db=$1
|
|
local charset=$2
|
|
# Avoid unsightly error when calling dropdb when the database doesn't exist
|
|
psql -h$DATABASE_HOST -U$DATABASE_USER -dtemplate1 -c "DROP DATABASE IF EXISTS $db"
|
|
createdb -h $DATABASE_HOST -U$DATABASE_USER -l C -T template0 -E $charset $db
|
|
}
|
|
|
|
function configure_database_postgresql {
|
|
echo_summary "Configuring and starting PostgreSQL"
|
|
if is_fedora; then
|
|
PG_HBA=/var/lib/pgsql/data/pg_hba.conf
|
|
PG_CONF=/var/lib/pgsql/data/postgresql.conf
|
|
if ! sudo [ -e $PG_HBA ]; then
|
|
if ! [[ $DISTRO =~ (rhel6) ]]; then
|
|
sudo postgresql-setup initdb
|
|
else
|
|
sudo service postgresql initdb
|
|
fi
|
|
fi
|
|
elif is_ubuntu; then
|
|
PG_DIR=`find /etc/postgresql -name pg_hba.conf|xargs dirname`
|
|
PG_HBA=$PG_DIR/pg_hba.conf
|
|
PG_CONF=$PG_DIR/postgresql.conf
|
|
elif is_suse; then
|
|
PG_HBA=/var/lib/pgsql/data/pg_hba.conf
|
|
PG_CONF=/var/lib/pgsql/data/postgresql.conf
|
|
# initdb is called when postgresql is first started
|
|
sudo [ -e $PG_HBA ] || start_service postgresql
|
|
else
|
|
exit_distro_not_supported "postgresql configuration"
|
|
fi
|
|
# Listen on all addresses
|
|
sudo sed -i "/listen_addresses/s/.*/listen_addresses = '*'/" $PG_CONF
|
|
# Set max_connections
|
|
sudo sed -i "/max_connections/s/.*/max_connections = $MAX_DB_CONNECTIONS/" $PG_CONF
|
|
# Do password auth from all IPv4 clients
|
|
sudo sed -i "/^host/s/all\s\+127.0.0.1\/32\s\+ident/$DATABASE_USER\t0.0.0.0\/0\tpassword/" $PG_HBA
|
|
# Do password auth for all IPv6 clients
|
|
sudo sed -i "/^host/s/all\s\+::1\/128\s\+ident/$DATABASE_USER\t::0\/0\tpassword/" $PG_HBA
|
|
restart_service postgresql
|
|
|
|
# Create the role if it's not here or else alter it.
|
|
root_roles=$(sudo -u root sudo -u postgres -i psql -t -c "SELECT 'HERE' from pg_roles where rolname='root'")
|
|
if [[ ${root_roles} == *HERE ]];then
|
|
sudo -u root sudo -u postgres -i psql -c "ALTER ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
|
|
else
|
|
sudo -u root sudo -u postgres -i psql -c "CREATE ROLE $DATABASE_USER WITH SUPERUSER LOGIN PASSWORD '$DATABASE_PASSWORD'"
|
|
fi
|
|
}
|
|
|
|
function install_database_postgresql {
|
|
echo_summary "Installing postgresql"
|
|
PGPASS=$HOME/.pgpass
|
|
if [[ ! -e $PGPASS ]]; then
|
|
cat <<EOF > $PGPASS
|
|
*:*:*:$DATABASE_USER:$DATABASE_PASSWORD
|
|
EOF
|
|
chmod 0600 $PGPASS
|
|
else
|
|
sed -i "s/:root:\w\+/:root:$DATABASE_PASSWORD/" $PGPASS
|
|
fi
|
|
if is_ubuntu; then
|
|
install_package postgresql
|
|
elif is_fedora || is_suse; then
|
|
install_package postgresql-server
|
|
else
|
|
exit_distro_not_supported "postgresql installation"
|
|
fi
|
|
}
|
|
|
|
function database_connection_url_postgresql {
|
|
local db=$1
|
|
echo "$BASE_SQL_CONN/$db?client_encoding=utf8"
|
|
}
|
|
|
|
|
|
# Restore xtrace
|
|
$PG_XTRACE
|
|
|
|
# Local variables:
|
|
# mode: shell-script
|
|
# End:
|