-
Notifications
You must be signed in to change notification settings - Fork 1
/
psql.sh
executable file
·47 lines (39 loc) · 1.32 KB
/
psql.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Exit if command fails
set -e
# Treat unset variables as errors
set -u
# Set user as current account
user=$(whoami)
# Install Postgres 10
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/PostgreSQL.list'
sudo apt update
sudo apt install -y postgresql-common
sudo apt install -y postgresql-10 postgresql-contrib libpq-dev
# Create superuser account as self for local management
sudo -u postgres createuser $user -s
# Set env vars for colors
YELLOW='\033[1;33m'
NC='\033[0m'
# Collect arguments from user
# Project specific values
printf "${YELLOW}Database name:\n${NC}"
read database
printf "${YELLOW}Username:\n${NC}"
read username
printf "${YELLOW}Password:\n${NC}"
read password
# Create database and user
RUN_ON_PSQL="psql -X -U $user --set ON_ERROR_STOP=on --set AUTOCOMMIT=off postgres"
$RUN_ON_PSQL <<SQL
CREATE DATABASE $database;
CREATE USER $username WITH PASSWORD '$password';
ALTER ROLE $username SET client_encoding TO 'utf8';
ALTER ROLE $username SET default_transaction_isolation TO 'read committed';
ALTER ROLE $username SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE $database TO $username;
ALTER USER $username CREATEDB;
commit;
SQL
exit 0