def create_database(self, delete_if_exists=False): if delete_if_exists and self.database_exists(): self.drop_database() if not self.database_exists(): sudo_pipeline("echo CREATE DATABASE %s WITH OWNER %s ENCODING \\'UNICODE\\' TEMPLATE template0 | psql" % ( self.database_name, self.user), user=self.superuser_login) return "Database was created" return "Database already exists"
def backup_database(self, filename, zip=False, folder=None): folder = folder or self.db_backup_folder with cuisine.cuisine_sudo(): cuisine.dir_ensure(folder, recursive=True, mode="777") file_full_path = "/".join([folder, filename]) with self.pg_pass(): sudo_pipeline(("pg_dump -O -x %s | gzip > %s" if zip else "pg_dump %s > %s") % (self.database_name, file_full_path), user=self.superuser_login)
def init_database(self, init_sql_file, delete_if_exists=False, unzip=False): self.create_database(delete_if_exists) if unzip: command = "cat %s | gunzip | psql %s -w -U %s" else: command = "cat %s | psql %s -w -U %s" with self.pg_pass(): run(command % (init_sql_file, self.database_name, self.user)) sudo_pipeline("echo GRANT ALL ON SCHEMA public TO %s | psql" % self.user, user=self.superuser_login) sudo_pipeline("echo ALTER DATABASE %s OWNER TO %s | psql" % (self.database_name, self.user), user=self.superuser_login)
def backup_database(self, filename, zip=False, folder=None, ignore_tables=None): folder = folder or self.db_backup_folder with cuisine.cuisine_sudo(): cuisine.dir_ensure(folder, recursive=True, mode="777") file_full_path = "/".join([folder, filename]) additional_argument = "" if ignore_tables: for table in ignore_tables: additional_argument = "%s -T '%s'" % (additional_argument, table) with self.pg_pass(): sudo_pipeline( ("pg_dump -O -x %s %s | gzip > %s" if zip else "pg_dump %s %s > %s") % (self.database_name, additional_argument, file_full_path), user=self.superuser_login, )
def create_user(self): sudo_pipeline("echo CREATE USER %s WITH PASSWORD \\'%s\\' | psql" % (self.user, self.password), user=self.superuser_login)