Skip to content

Instantly share code, notes, and snippets.

@jacekkopecky
Last active September 18, 2019 12:58
Show Gist options
  • Save jacekkopecky/6c13a4f6a362c03cfc64c68d19f91d28 to your computer and use it in GitHub Desktop.
Save jacekkopecky/6c13a4f6a362c03cfc64c68d19f91d28 to your computer and use it in GitHub Desktop.
create postgres user and database based on hostname – used in OneServerPerStudent at Uni of Portsmouth
#!/bin/sh
echo creating PostgreSQL user and database for the main user of this machine
# wait for postgres to start
while ! su -c "echo testing postgres connection; psql -c '\\c'" postgres
do
echo waiting for postgres to start
sleep 10
done
# check hostname again in case it wasn't ready when the script started
host='"'`hostname`'"'
echo "creating PostgreSQL user and database for $host - ignore following issues in case the user already exists"
su -c "psql -c 'create user $host superuser login password '\'root\'" postgres
su -c "psql -c 'create database $host owner $host'" postgres
@jacekkopecky
Copy link
Author

jacekkopecky commented Sep 9, 2019

use like this:

curl https://gist.githubusercontent.com/jacekkopecky/6c13a4f6a362c03cfc64c68d19f91d28/raw/a6aea1c519a172a1ca3ddc9ac7e1af4255453b6c/postgres-create-host-user -o /etc/init.d/postgres-create-host-user
chmod +x /etc/init.d/postgres-create-host-user
ln -s /etc/init.d/postgres-create-host-user /etc/rc5.d/S99postgres-create-host-user

then it wants to be tested at osps-devel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment