| 1 | #!/bin/sh -e |
| 2 | |
| 3 | export PGPORT="$2" |
| 4 | |
| 5 | case $1 in |
| 6 | adduser) |
| 7 | USERNAME=$3 |
| 8 | |
| 9 | sudo -u postgres psql -h postgres -p $PGPORT -c "CREATE USER $USERNAME" template1 |
| 10 | sudo -u postgres psql -h postgres -p $PGPORT -c "ALTER TABLESPACE user_$USERNAME OWNER TO $USERNAME" |
| 11 | ;; |
| 12 | |
| 13 | createdb) |
| 14 | USERNAME=$3 |
| 15 | DBNAME_BASE=$4 |
| 16 | ENCODING=$5 |
| 17 | DBNAME="${USERNAME}_${DBNAME_BASE}" |
| 18 | |
| 19 | if [ -n "$ENCODING" ]; then |
| 20 | ENCODING="-E $ENCODING" |
| 21 | fi |
| 22 | |
| 23 | sudo -u postgres createdb -h postgres -p $PGPORT -O $USERNAME -D user_$USERNAME $ENCODING $DBNAME |
| 24 | ;; |
| 25 | |
| 26 | dropdb) |
| 27 | USERNAME=$3 |
| 28 | DBNAME_BASE=$4 |
| 29 | DBNAME="${USERNAME}_${DBNAME_BASE}" |
| 30 | |
| 31 | sudo -u postgres dropdb -h postgres -p $PGPORT $DBNAME |
| 32 | ;; |
| 33 | |
| 34 | *) |
| 35 | echo "Usage: domtool-postgres [adduser <user> | createdb <user> <db> | dropdb <user> <db>]" |
| 36 | ;; |
| 37 | esac |