Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/dev/smc/start_postgres.py
Views: 687
#!/usr/bin/env python12"""3This is a script for starting PostgreSQL for SMC main site deployment, at4least until we change the database to use Kubernetes.5"""67import os, sys, time89path = os.path.split(os.path.realpath(__file__))[0]; os.chdir(path); sys.path.insert(0, path)1011PG_DATA = os.path.join(path, "postgres_data")1213def cmd(s):14print(s)15if os.system(s):16raise RuntimeError1718def stop_server():19try:20cmd("kill %s"%(open(os.path.join(PG_DATA, 'postmaster.pid')).read().split()[0]))21time.sleep(3)22except Exception as err:23print("WARNING ", err)242526if __name__ == '__main__':27if not os.path.exists(PG_DATA):28# Create the database directory file structure29cmd("pg_ctl init -D '%s'"%PG_DATA)3031# Set database to only use local sockets for communication (with no password)32open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write('local all all trust\n')33cmd("chmod og-rwx '%s'"%PG_DATA) # just in case -- be paranoid...3435# Start database running in background as daemon36cmd("postgres -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))37time.sleep(5)3839# Create the smc user (with no password -- you better do that!!)40cmd("createuser -sE smc")4142# Stop database daemon43stop_server()4445# Set database so only way to connect is as 'smc' user via encrypted password.46# (TODO: Note -- connection (so data) isn't necessarily encrypted unless we build47# postgreSQL properly -- see https://www.postgresql.org/docs/9.6/static/auth-pg-hba-conf.html)48open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write('host all smc all md5\n')4950# Start database daemon listening on all network interfaces.51cmd("postgres -h 0.0.0.0 -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))525354