Ok, demorei um pouco para fazer isso funcionar, mas aqui está:
def run(command, action_descr, env=None):
print '\n%s' % termcolor.colorize(action_descr, termcolor.c.YELLOW)
print '$ %s' % termcolor.colorize(command, termcolor.c.YELLOW)
try:
output = subprocess.check_output(command, env=env, shell=True)
except subprocess.CalledProcessError as exc:
print termcolor.colorize('FAIL:\n%s' % exc.output, termcolor.c.RED, True)
raise
else:
print termcolor.colorize('OK', termcolor.c.GREEN, True)
return output
def create_test_db_template():
"""Create test template database.
"""
PG_DIR = run('pg_config --bindir', 'Getting the location of Postgresql executables').strip()
PG_CTL = os.path.join(PG_DIR, 'pg_ctl')
test_db_directory = settings.get('tests', 'db_directory')
env = os.environ.copy()
env['PGDATA'] = test_db_directory
env['PGPORT'] = settings.get_test_db_port()
try:
status = run('%s status' % PG_CTL, 'Checking previous test server status', env)
except subprocess.CalledProcessError:
pass
else:
print status
run('%s stop' % PG_CTL, 'Stopping previous instance of test server', env)
run('rm -rf %s' % test_db_directory, 'Deleting server data directory')
run('mkdir -p %s' % test_db_directory, 'Creating server data directory')
run('%s initdb -o "--auth-host=trust --nosync"' % PG_CTL,
'Initializing test server and data directory', env)
pg_log_path = os.path.join(test_db_directory, 'logfile')
try:
run('%s start -w --log %s' % (PG_CTL, pg_log_path), 'Starting test server', env)
except subprocess.CalledProcessError:
print termcolor.colorize(open(pg_log_path).read(), termcolor.c.RED)
raise
for db_name, db_queries in INIT_DB_SQL:
for db_query in db_queries:
run('psql %s -c "%s"' % (db_name, db_query), 'Creating template database', env)
test_template_db_url = 'postgresql://127.0.0.1:%s/%s' % (env['PGPORT'], TEST_TEMPLATE_DB_NAME)
postgres_db_url = 'postgresql://127.0.0.1:%s/postgres' % (env['PGPORT'])
...
Isso tem muito código adicional, mas você deve ter a idéia.
A única coisa a fazer manualmente é fazer:
sudo chmod 777 /var/run/postgresql
Esta solução está sem o disco RAM, mas a maior parte da solução para iniciar o servidor Postgresql no espaço do usuário está aqui.
Para executar tudo isso em um disco RAM, faça test_db_directory = 'dev/shm/pgdata/'