Last active
August 21, 2019 07:17
-
-
Save benbonnet/541512ed035e590aa0ad16d9129415d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createPgInstance() { | |
export BASE_PATH=$HOME/postgres-storage; | |
export INSTANCES_COUNT=$(find $BASE_PATH -maxdepth 1 -type d | wc -w); | |
export PORT=$1 | |
export PG_USERNAME=$2 | |
export PG_PASSWORD=$3 | |
export DBNAME=postgres$PORT | |
export INSTALLFOLDER=$BASE_PATH/$DBNAME/data; | |
export LOG_FOLDER=$BASE_PATH/$DBNAME/$DBNAME.log; | |
export SYSTEMDFILE=/etc/systemd/system/$DBNAME.service; | |
sudo -H -u postgres bash -c "/usr/lib/postgresql/10/bin/initdb $INSTALLFOLDER"; | |
sudo sed -i "s|.*listen_addresses =.*|listen_addresses = '*'|" $INSTALLFOLDER/postgresql.conf | |
sudo sed -i "s|.*port = 5432|port = $PORT|" $INSTALLFOLDER/postgresql.conf | |
echo "host all all 0.0.0.0/0 md5" | sudo tee -a $INSTALLFOLDER/pg_hba.conf | |
echo "host all all ::/0 md5" | sudo tee -a $INSTALLFOLDER/pg_hba.conf | |
sudo cp -p $HOME/postgres-storage/postgres.service.orig $SYSTEMDFILE; | |
sudo sed -i "s|ExecStart=.*|ExecStart=/usr/lib/postgresql/10/bin/pg_ctl start -D $INSTALLFOLDER -o '-p $PORT' -s -w -l $LOG_FOLDER|" $SYSTEMDFILE; | |
sudo sed -i "s|ExecStop=.*|ExecStop=/usr/lib/postgresql/10/bin/pg_ctl stop -D $INSTALLFOLDER -o '-p $PORT' -s -w -l $LOG_FOLDER|" $SYSTEMDFILE; | |
sudo sed -i "s|ExecReload=.*|ExecReload=/usr/lib/postgresql/10/bin/pg_ctl reload -D $INSTALLFOLDER -o '-p $PORT' -s -w -l $LOG_FOLDER|" $SYSTEMDFILE; | |
sudo systemctl enable $DBNAME.service; | |
sudo systemctl start $DBNAME.service; | |
psql -h 127.0.0.1 -p $PORT -U postgres -c "CREATE USER $PG_USERNAME;" | |
psql -h 127.0.0.1 -p $PORT -U postgres -c "ALTER USER $PG_USERNAME WITH SUPERUSER;" | |
psql -h 127.0.0.1 -p $PORT -U postgres -c "ALTER USER $PG_USERNAME WITH PASSWORD '$PG_PASSWORD';" | |
psql -h 127.0.0.1 -p $PORT -U postgres -c "CREATE DATABASE $PG_USERNAME"; | |
} | |
createPgInstance "$@"; exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment