-
Notifications
You must be signed in to change notification settings - Fork 206
/
start_postgres.py
executable file
·72 lines (58 loc) · 2.61 KB
/
start_postgres.py
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
#!/usr/bin/env python3
"""
This is a script for starting postgres for development purposes
in an SMC project.
"""
import os, sys, time, util
path = os.path.split(os.path.realpath(__file__))[0]
os.chdir(path)
sys.path.insert(0, path)
PG_DATA = os.path.abspath(os.path.join(path, "../../data/postgres"))
PGHOST = os.environ.get("PGHOST", "")
if not os.path.exists(PG_DATA):
util.cmd("pg_ctl init -D '%s'" % PG_DATA)
# Lock down authentication so it is ONLY via unix socket
open(os.path.join(PG_DATA, 'pg_hba.conf'), 'w').write("""
# This is safe since we only enable a socket protected by filesystem permissions:
local all all trust
# You can uncomment this and comment out the above if you want to test password auth.
#local all all md5
""")
conf = os.path.join(PG_DATA, 'postgresql.conf')
s = open(conf).read()
s += '\n'
# The very first time running this script, if PGHOST is NOT set, we do the
# following (we want to allow users to override this via setting PGHOST
# the first time, since this approach sucks in some situations):
# Make it so the socket is in this subdirectory, so that it is
# protected by UNIX permissions. This approach avoids any need
# for accounts/passwords for development and the Docker image.
# Move the default directory where the socket is from /tmp to right here.
if PGHOST:
socket_dir = PGHOST
else:
socket_dir = os.path.join(PG_DATA, 'socket')
# Increase max connections since in dev mode nextjs is constantly restarting causing lots of hanging connections (TODO)
s += "unix_socket_directories = '%s'\nlisten_addresses=''\nmax_connections=1000\n" % socket_dir
if not os.path.exists(socket_dir):
os.makedirs(socket_dir)
util.cmd("chmod og-rwx '%s'" % PG_DATA) # just in case -- be paranoid...
open(conf, 'w').write(s)
# Create script so that clients will know where socket dir is.
open("postgres-env", 'w').write("""#!/bin/sh
export PGUSER='smc'
export PGHOST='%s'
""" % socket_dir)
util.cmd('chmod +x postgres-env')
# Start database running in background as daemon
util.cmd("postgres -D '%s' >%s/postgres.log 2>&1 &" % (PG_DATA, PG_DATA))
time.sleep(5)
# Create the smc user with no password (not needed since we are using local file permissions)
util.cmd("unset PGUSER; unset PGHOST; createuser -h '%s' -sE smc" %
socket_dir)
# Stop database daemon
util.cmd("kill %s" %
(open(os.path.join(PG_DATA, 'postmaster.pid')).read().split()[0]))
# Let it die and remove lock file.
time.sleep(3)
util.cmd("postgres -D '%s'" % PG_DATA)