示例#1
0
    def verify_hstore_extension(self):
        """
        Installs the hstore extension to dbname if not already present.

        Installation takes place in the namespace mentioned first in the
        search path, which should be the manage namespace in NAV's case.

        """
        postgres_opts = ConnectionParameters.for_postgres_user()
        postgres_opts.export(os.environ)

        self.cursor.execute(
            "SELECT COUNT(*) FROM pg_extension WHERE extname='hstore'")
        count, = self.cursor.fetchone()
        if count > 0:
            return

        print "Creating hstore extension in database {0}".format(
            self.connect_options.dbname)

        trap_and_die(subprocess.CalledProcessError,
                     "Failed to install the hstore extension, maybe you need "
                     "to run as the postgres superuser?",
                     check_call,
                     ["psql", "--quiet", "-c", "CREATE EXTENSION hstore;",
                      self.connect_options.dbname])
示例#2
0
    def verify_hstore_extension(self):
        """
        Installs the hstore extension to dbname if not already present.

        Installation takes place in the namespace mentioned first in the
        search path, which should be the manage namespace in NAV's case.

        """
        postgres_opts = ConnectionParameters.for_postgres_user()
        postgres_opts.export(os.environ)

        self.cursor.execute(
            "SELECT COUNT(*) FROM pg_extension WHERE extname='hstore'")
        count, = self.cursor.fetchone()
        if count > 0:
            return

        print("Creating hstore extension in database {0}".format(
            self.connect_options.dbname))

        trap_and_die(
            subprocess.CalledProcessError,
            "Failed to install the hstore extension, maybe you need "
            "to run as the postgres superuser?", check_call, [
                "psql", "--quiet", "-c", "CREATE EXTENSION hstore "
                "WITH SCHEMA manage;", self.connect_options.dbname
            ])
示例#3
0
def drop_database():
    """Drops an existing database using PostgreSQL command line clients"""
    nav_opts = ConnectionParameters.from_config()
    postgres_opts = ConnectionParameters.for_postgres_user()
    postgres_opts.export(os.environ)

    print("Dropping database %s" % nav_opts.dbname)
    trap_and_die(subprocess.CalledProcessError,
                 "Failed to drop database %s" % nav_opts.dbname, check_call,
                 ["dropdb", nav_opts.dbname])
示例#4
0
def drop_database():
    """Drops an existing database using PostgreSQL command line clients"""
    nav_opts = ConnectionParameters.from_config()
    postgres_opts = ConnectionParameters.for_postgres_user()
    postgres_opts.export(os.environ)

    print "Dropping database %s" % nav_opts.dbname
    trap_and_die(subprocess.CalledProcessError,
                 "Failed to drop database %s" % nav_opts.dbname,
                 check_call, ["dropdb", nav_opts.dbname])
示例#5
0
def restore_from_dump(filename):
    """Restores a NAV database from an SQL dump produced by pg_dump"""
    postgres_opts = ConnectionParameters.for_postgres_user()
    postgres_opts.export(os.environ)
    nav_opts = ConnectionParameters.from_config()

    print("Restoring database %s from file %s" % (nav_opts.dbname, filename))
    trap_and_die(
        subprocess.CalledProcessError,
        "Failed to restore database %s from file %s" %
        (nav_opts.dbname, filename), check_call,
        ["psql", "--quiet", "-f", filename, nav_opts.dbname])
示例#6
0
def restore_from_dump(filename):
    """Restores a NAV database from an SQL dump produced by pg_dump"""
    postgres_opts = ConnectionParameters.for_postgres_user()
    postgres_opts.export(os.environ)
    nav_opts = ConnectionParameters.from_config()

    print "Restoring database %s from file %s" % (nav_opts.dbname, filename)
    trap_and_die(
        subprocess.CalledProcessError,
        "Failed to restore database %s from file %s" % (nav_opts.dbname,
                                                        filename),
        check_call, ["psql", "--quiet", "-f", filename, nav_opts.dbname])
示例#7
0
文件: pgsync.py 项目: Cloudxtreme/nav
def create_database():
    """Create a database using PostgreSQL command line clients"""
    nav_opts = ConnectionParameters.from_config()
    postgres_opts = ConnectionParameters.for_postgres_user()
    postgres_opts.export(os.environ)

    if not user_exists(nav_opts.user):
        create_user(nav_opts.user, nav_opts.password)

    print "Creating database %s owned by %s" % (nav_opts.dbname, nav_opts.user)
    trap_and_die(subprocess.CalledProcessError,
                 "Failed creating database %s" % nav_opts.dbname,
                 check_call, ["createdb",
                              "--owner=%s" % nav_opts.user,
                              "--encoding=utf-8", nav_opts.dbname])
    install_pl_pgsql(nav_opts.dbname)