def database(name, owner, template='template0', encoding='UTF8', locale='en_US.UTF-8'): """ Require a PostgreSQL database. :: from fabtools import require require.postgres.database('myapp', owner='dbuser') """ if not database_exists(name): if locale not in run('locale -a').split(): require_locale(locale) restarted(_service_name()) create_database(name, owner, template=template, encoding=encoding, locale=locale)
def database(name, owner, template='template0', encoding='UTF8', locale='en_US.UTF-8', allow_restart=False): """ Require a PostgreSQL database. :: from fabtools import require require.postgres.database('myapp', owner='dbuser') """ locale_transform = lambda l: l.strip().lower().replace('-', '') if not database_exists(name): locales = map( locale_transform, run('locale -a').split() ) if locale_transform(locale) not in locales: if not allow_restart: abort( 'New locale "{}" must be installed and ' 'postgres must be restarted after that'.format( locale ) ) require_locale(locale) restarted(_service_name()) create_database(name, owner, template=template, encoding=encoding, locale=locale)
def database(name, owner, template='template0', encoding='UTF8', locale='en_US.UTF-8'): """ Require a PostgreSQL database. :: from fabtools import require require.postgres.database('myapp', owner='dbuser') """ if not database_exists(name): with watch('/etc/locale.gen') as locales: require_locale(locale) if locales.changed: restarted(_service_name()) create_database(name, owner, template=template, encoding=encoding, locale=locale)
def setup_postgres(): """Initial postgres setup.""" if not db.user_exists(env.database_user): if 'database_pw' not in env: prompt('PostgreSQL database password:'******'database_pw') create_user(env.database_user, password=env.database_pw, encrypted_password=True) if not database_exists(env.database_name): create_database(env.database_name, env.database_user)
def restore(self, filename): if not confirm('Are you sure to delete database?', default=False): return with warn_only(): sudo('sudo -u postgres dropdb {}'.format(self.database)) funcs.create_database( self.database, self.user, locale='ja_JP.utf8') sudo('bzip2 -d -c {} | sudo -u postgres psql -d {}'.format( filename, self.database))
def postgres_create(): """ Create postgres objects, including owner, databases, and schemas. """ if not pg_user_exists(env.db_user): create_pg_user(env.db_user, password=env.db_password) if not database_exists(env.db_name): create_database(env.db_name, owner=env.db_user)
def run(self): user = env['user'] if self.user: user = self.user if not funcs.user_exists(user): funcs.create_user(user, self.password) if not funcs.database_exists(self.database): funcs.create_database( self.database, user, locale='ja_JP.utf8')
def server_prepare(): """ installing all necessary packages """ apt_packages = ('libpq-dev', 'python3-dev', 'libssl-dev', 'lib32ncurses5-dev', 'python3', 'postgresql', 'postgis',) sudo('apt-get install %s' % " ".join(apt_packages)) sudo('easy_install pip') """ db routines """ db_env = DB[env.environment] if not postgres.user_exists(db_env["user"]): postgres.create_user(db_env["user"], db_env["password"], createdb=True, createrole=True) if not postgres.database_exists(db_env['db']): postgres.create_database(db_env['db'], owner=db_env["user"])
def set_db_permissions(): """Set the db so user wsgi has all permissions. """ user = "******" dbname = "changelog" if not user_exists(user): create_user(user, password="******") if not database_exists(dbname): create_database(dbname, user) grant_sql = "GRANT ALL ON schema public to %s;" % user # assumption is env.repo_alias is also database name run('psql %s -c "%s"' % (dbname, grant_sql)) grant_sql = "GRANT ALL ON ALL TABLES IN schema public to %s;" % user # assumption is env.repo_alias is also database name run('psql %s -c "%s"' % (dbname, grant_sql)) grant_sql = "GRANT ALL ON ALL SEQUENCES IN schema public to %s;" % user run('psql %s -c "%s"' % (dbname, grant_sql))
def database(name, owner, template="template0", encoding="UTF8", locale="en_US.UTF-8"): """ Require a PostgreSQL database. :: from fabtools import require require.postgres.database('myapp', owner='dbuser') """ if not database_exists(name): if locale not in run("locale -a").split(): require_locale(locale) restarted(_service_name()) create_database(name, owner, template=template, encoding=encoding, locale=locale)
def set_db_permissions(): """Set the db so user wsgi has all permissions. """ user = '******' dbname = 'changelog' if not user_exists(user): create_user(user, password='******') if not database_exists(dbname): create_database(dbname, user) grant_sql = 'GRANT ALL ON schema public to %s;' % user # assumption is env.repo_alias is also database name run('psql %s -c "%s"' % (dbname, grant_sql)) grant_sql = ( 'GRANT ALL ON ALL TABLES IN schema public to %s;' % user) # assumption is env.repo_alias is also database name run('psql %s -c "%s"' % (dbname, grant_sql)) grant_sql = ( 'GRANT ALL ON ALL SEQUENCES IN schema public to %s;' % user) run('psql %s -c "%s"' % (dbname, grant_sql))
def create_pgsql_user(username, password=None): if postgres.user_exists(username): raise KeyError("Username {} is taken".format(username)) if password is None: password = gen_pass(10) postgres.create_user(username, password=password, createdb=True) db_not_created = True db_name = "" while db_not_created: db_name = gen_pass(12) + "_DB" if not postgres.database_exists(db_name): postgres.create_database(db_name, owner=username) db_not_created = False print c.blue("Username="******"Password="******"DB Name =", bold=True),\ c.green(db_name, bold=True) return username, password, db_name