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/minimal/start_postgres.py
Views: 687
#!/usr/bin/env python12"""3This is a script for starting postgres for development purposes on a laptop.4"""56import os, sys, time, util78path = os.path.split(os.path.realpath(__file__))[0]; os.chdir(path); sys.path.insert(0, path)910PG_DATA = os.path.join(path, "postgres_data")1112if not os.path.exists(PG_DATA):13util.cmd("pg_ctl init -D '%s'"%PG_DATA)1415# Lock down authentication so it is ONLY via unix socket16open(os.path.join(PG_DATA,'pg_hba.conf'), 'w').write(17"""18# This is safe since we only enable a socket protected by file system permissions:19local all all trust2021# You can uncomment this and comment out the above if you want to test password auth.22#local all all md523""")2425# Make it so the socket is in this subdirectory, so that it is26# protected by UNIX permissions. This approach avoids any need27# for accounts/passwords for development and the Docker image.28conf = os.path.join(PG_DATA, 'postgresql.conf')29s = open(conf).read()30s += '\n'3132# Move the default directory where the socket is from /tmp to right here.33socket_dir = os.path.join(PG_DATA, 'socket')34s += "unix_socket_directories = '%s'\nlisten_addresses=''\n"%socket_dir35os.makedirs(socket_dir)36util.cmd("chmod og-rwx '%s'"%PG_DATA) # just in case -- be paranoid...37open(conf,'w').write(s)3839# Create script so that clients will know where socket dir is.40open("postgres-env", 'w').write("""#!/bin/sh41export PGUSER='smc'42export PGHOST='%s'43"""%socket_dir)4445util.cmd('chmod +x postgres-env')4647# Start database running in background as daemon48util.cmd("postgres -D '%s' >%s/postgres.log 2>&1 &"%(PG_DATA, PG_DATA))49time.sleep(5)5051# Create the smc user with no password (not needed since we are using local file permissions)52util.cmd("unset PGUSER; unset PGHOST; createuser -h '%s' -sE smc"%socket_dir)5354# Stop database daemon55util.cmd("kill %s"%(open(os.path.join(PG_DATA, 'postmaster.pid')).read().split()[0]))56# Let it die and remove lock file.57time.sleep(3)585960util.cmd("postgres -D '%s'"%PG_DATA)616263