Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 157ab27b8f
Fetching contributors…

Cannot retrieve contributors at this time

38 lines (30 sloc) 1.344 kB
# -*- coding: utf-8 -*-
postgres sql functions for fabric
from __future__ import with_statement
from fabric.api import *
def sql_run(data):
''' run an sql command as sudo -u postgres on user@host '''
psqlcmd = '/usr/bin/psql -d template1 -c '
sudo("".join((psqlcmd,'"',data,'"')), shell=False, user="postgres")
def sql_dropdb(dbname):
""" Purges Database from SQL Server; usage: dbname"""
with settings(warn_only=True):
sql_run('drop database %s;' % (dbname))
def sql_dropuser(dbuser):
""" Purges database user from SQL Server; usage: dbuser"""
with settings(warn_only=True):
sql_run('drop user %s;' % (dbuser))
def sql_createdb(dbname, dbuser, dbpassword=None):
''' Creates an postgres database dbname, owned by user dbuser; if dbpassword is set, then user will be created too'''
with settings(warn_only=True):
if dbpassword:
sql_run('create database %s with owner %s;' % (dbname, dbuser))
def sql_createuser(dbuser, dbpassword):
''' Creates an postgres database user, and sets password to dbpassword; is idempotent'''
with settings(warn_only=True):
sql_run('create user %s;' % (dbuser))
sql_run('alter user %s with password \'%s\';' % (dbuser, dbpassword))
Jump to Line
Something went wrong with that request. Please try again.