用于开发测试的构建PostgreSQL数据库的工具。
项目描述
HitchBuildPg
一个小型、自包含的Python库,用于在本地构建PostgreSQL数据库,首次运行时会
- 
下载并编译指定版本的PostgreSQL。 
- 
通过一系列SQL命令和恢复数据库转储文件来构建数据库。 
- 
通过复制文件夹来对新构建的数据库文件进行快照。 
在后续构建中,它将跳过下载、编译PostgreSQL和从.sql文件恢复数据库的漫长、昂贵的步骤,而只是覆盖数据文件。
import hitchbuildpg
postgres_version = "10.10"
    
pgapp_dir = "{}/postgres-{}".format(share, postgres_version)
pgapp = hitchbuildpg.PostgresApp(pgapp_dir, postgres_version)
class DataBuild(hitchbuildpg.DataBuild):
    def run(self):
        self.run_sql_as_root("create user myuser with password 'mypassword';")
        self.run_sql_as_root("create database mydb with owner myuser;")
        self.load_database_dump(
            database="mydb",
            username="myuser",
            password="mypassword",
            filename="dump.sql"
        )
pgdata = hitchbuildpg.PostgresDatafiles(
    "./myappdata",
    pgapp,
    DataBuild(),
)
pgdata.ensure_built()
db_service = pgdata.server()
db_service.start()
psql = db_service.psql(
    "-U", "myuser", "-p", "15432", "-d", "mydb",
).with_env(PG_PASSWORD="mypassword")
psql("-c", "select name from cities where location = 'GB';").run()
# Prints output of SQL statement
    
psql("-c", "delete from cities where location = 'GB';").run()
      
db_service.stop()