Repository navigation
Expand file tree
/
Copy pathstart_postgres.sh
More file actions
115 lines (91 loc) · 2.53 KB
/
Copy pathstart_postgres.sh
File metadata and controls
115 lines (91 loc) · 2.53 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
echo ""
echo "This script configures and starts Postgresql"
echo "inside of a Docker container."
echo "--------------------------------------------"
# Default parameters
#
USER=$POSTGRESQL_USER
PASSWORD=$POSTGRESQL_PASSWORD
DATADIR=${POSTGRESQL_DATADIR:=/var/lib/postgresql/9.1/main}
BINDIR=${POSTGRESQL_BINDIR:=/usr/lib/postgresql/9.1/bin}
CONFIG_FILE=${POSTGRESQL_CONFIG_FILE:=/etc/postgresql/9.1/main/postgresql.conf}
HBA_FILE=${POSTGRESQL_HBA_FILE:=/etc/postgresql/9.1/main/pg_hba.conf}
IDENT_FILE=${POSTGRESQL_IDENT_FILE:=/etc/postgresql/9.1/main/pg_ident.conf}
MAX_CONNECTIONS=${POSTGRES_MAX_CONNECTIONS:=60}
# Custom die function.
#
die() { echo >&2 -e "\nRUN ERROR: $@\n"; exit 1; }
# Parse the command line flags.
#
while getopts ":u:p:d:c:h:s:" opt; do
case $opt in
u)
USER=${OPTARG}
;;
p)
PASSWORD=${OPTARG}
;;
d)
DATADIR=${OPTARG}
;;
c)
CONFIG_FILE=${OPTARG}
;;
h)
HBA_FILE=${OPTARG}
;;
i)
IDENT_FILE=${OPTARG}
;;
\?)
die "Invalid option: -$OPTARG"
;;
esac
done
# Create a shortcut postgres command with all
# the configuration options specified.
#
PGCMD=$BINDIR/postgres
PGARGS="-c config_file=$CONFIG_FILE -c data_directory=$DATADIR -c hba_file=$HBA_FILE -c ident_file=$IDENT_FILE"
# Both $USER and $PASSWORD must be specified if
# one of them is specified.
#
if [[ -z $USER ]]; then
if [[ ! -z $PASSWORD ]]; then
die "If you give a PASSWORD, you must supply a USER!"
fi
else
if [[ -z $PASSWORD ]]; then
die "If you give a USER, you must supply a PASSWORD!"
fi
fi
# If DATADIR does not exist, create it
#
if [ ! -d $DATADIR ]; then
echo "Creating Postgres data at $DATADIR"
mkdir -p $DATADIR
fi
# If DATADIR has no content, initialize it
#
if [ ! "$(ls -A $DATADIR)" ]; then
echo "Initializing Postgres Database at $DATADIR"
chown -R postgres $DATADIR
su postgres sh -c "$BINDIR/initdb $DATADIR"
fi
# Create a user
#
if [[ ! -z $USER ]]; then
echo "Setting up Postgresql user '$USER' with password '$PASSWORD'"
echo "$PGCMD --single $PGARGS"
su postgres sh -c "$PGCMD --single $PGARGS" <<< "CREATE USER $USER WITH SUPERUSER PASSWORD '$PASSWORD';"
fi
# Start the Postgresql process
#
echo "Starting Postgresql with the following options:"
echo -e "\t data_directory=$DATADIR"
echo -e "\t config_file=$CONFIG_FILE"
echo -e "\t hba_file=$HBA_FILE"
echo -e "\t ident_file=$IDENT_FILE"
echo -e "\t listen_addresses='*'"
su postgres sh -c "$PGCMD $PGARGS -c listen_addresses='*' -N $MAX_CONNECTIONS"