2018-09-13 07:43:21 +00:00
|
|
|
#!/usr/bin/env bash
|
2014-10-19 15:27:33 +00:00
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
|
|
|
if [ $# -lt 1 ]
|
|
|
|
then
|
|
|
|
echo "Usage: $0 <username> [<database>]"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ $EUID -eq 0 ]
|
|
|
|
then
|
|
|
|
echo "Must be root!" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
USER="$1"
|
|
|
|
DBPASSWORD="$(tr -dc A-Za-z0-9 < /dev/urandom | head -c 64)"
|
|
|
|
DATABASE="${2:-$USER}"
|
|
|
|
|
2018-09-13 07:43:21 +00:00
|
|
|
lxc-attach -n postgres -- su postgres -c psql <<EOF
|
2014-10-19 15:27:33 +00:00
|
|
|
create user "$USER";
|
|
|
|
alter user "$USER" with password '$DBPASSWORD';
|
|
|
|
create database "$DATABASE" with owner "$USER";
|
|
|
|
grant all privileges on database "$DATABASE" to "$USER";
|
|
|
|
EOF
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "Password for user $USER is:"
|
|
|
|
echo
|
|
|
|
echo $DBPASSWORD
|
|
|
|
echo
|