def db(password, db_name=None, user=None): """ Install and setup a postgres DB, creating a database with rights given to the current user. """ db_name = db_name or env.project_name user = user or db_name require.deb.packages(['python-psycopg2', 'libpq-dev']) require.postgres.server() require.postgres.user(user, password=password) require.postgres.database(db_name, user) # add power to manage the DB to the user so it can create and delete # test database sql = " ALTER ROLE %s WITH CREATEDB;" % user postgres._run_as_pg('''psql -t -A -c "%s"''' % sql) # add pwd to local_settings if not contains(env.local_settings, password): db_settings = "DATABASES['default']['PASSWORD'] = '''{password}'''" append(env.local_settings, db_settings.format(password=password)) # allow connection using password sed('/etc/postgresql/9.1/main/pg_hba.conf', 'local all all peer', 'local all all md5', use_sudo=True) require.service.restarted('postgresql')
def init_postgres(encoding='UTF8', locale='en_US.UTF-8'): """ Arch postgresql daemon needs to be initiated once with its own user. """ require_locale(locale) cmd = "initdb --locale {0} -E {1} -D '/var/lib/postgres/data/'".format( locale, encoding ) _run_as_pg(cmd)
def _create_database_postgis(self, database_settings): self._create_database_postgresql(database_settings) major_postgis_version = int(env.get("postgis_version").split(".")[0]) if major_postgis_version < 2: raise ValueError("PostGIS versions minor then 2 are not supported.") with settings(warn_only=True): result = _run_as_pg(self.POSTGIS_EXTENSION_COMMAND.format(database_settings["NAME"])) if result.failed: _run_as_pg(self.DROP_DATABASE_COMMAND.format(database_settings["NAME"]))
def _create_database_postgis(self, database_settings): self._create_database_postgresql(database_settings) major_postgis_version = int(env.get("postgis_version").split(".")[0]) if major_postgis_version < 2: raise ValueError( "PostGIS versions minor then 2 are not supported.") with settings(warn_only=True): result = _run_as_pg( self.POSTGIS_EXTENSION_COMMAND.format( database_settings["NAME"])) if result.failed: _run_as_pg( self.DROP_DATABASE_COMMAND.format( database_settings["NAME"]))
def configure_postgres_role(): if user_exists("postgres"): _run_as_pg( "psql -U postgres -c \"ALTER USER postgres WITH password '{0}'\"". format(env.postgresql_role_password))
def configure_postgres_role(): if user_exists("postgres"): _run_as_pg("psql -U postgres -c \"ALTER USER postgres WITH password '{0}'\"".format(env.postgresql_role_password))
def destroy_database(database): confirmation = confirm('This will detroy the database %s' ' for good. Are you sure?' % database, default=False) if confirmation: sql = 'DROP DATABASE %s' % database postgres._run_as_pg('''psql -t -A -c "%s"''' % sql)
def database_drop(): postgres._run_as_pg("""psql -c 'drop database %s'""" % (DATABASE,))
def postgresql_user_drop(): postgres._run_as_pg("""psql -c 'drop user %s'""" % (DATABASE_OWNER))