【问题标题】:How to deploy PostgreSQL schema to Google Cloud SQL database using Pulumi?如何使用 Pulumi 将 PostgreSQL 架构部署到 Google Cloud SQL 数据库?
【发布时间】:2021-06-15 02:36:50
【问题描述】:

我正在尝试使用 Pulumi 初始化托管 PostgreSQL 数据库。 PostgreSQL 服务器本身由 Google Cloud SQL 托管和管理,但我使用 Pulumi 进行设置。

我可以成功创建数据库,但是我不知道如何用我的架构、用户、表等实际初始化它。有人知道如何实现吗?

我相信我需要使用Postgres provider,类似于他们在this 教程或this 示例中为MySQL 所做的。下面的代码显示了我到目前为止所拥有的:

# Create database resource on Google Cloud
instance = sql.DatabaseInstance(  # This works
    "db-instance",
    name="db-instance",
    database_version="POSTGRES_12",
    region="europe-west4",
    project=project,
    settings=sql.DatabaseInstanceSettingsArgs(
        tier="db-g1-small",  # Or: db-n1-standard-4
        activation_policy="ALWAYS",
        availability_type="REGIONAL",
        backup_configuration={
            "enabled": True,
        }
    ),
    deletion_protection=False,
)

database = sql.Database(  # This works as well
    "db",
    name="db",
    instance=instance.name,
    project=project,
    charset="UTF-8",
)

# The below should create a table such as 
#     CREATE TABLE users (id uuid, email varchar(255), api_key varchar(255);
# How to tell it to use this SQL script?
# How to connect it to the above created PostgreSQL resource?
postgres = pg.Database(  # This doesn't work
    f"users",
    name="users",
    is_template=False,
)

【问题讨论】:

    标签: database postgresql devops pulumi


    【解决方案1】:

    这里是sample code,解释了我们如何设置所有内容,包括使用 Pulumi 创建/删除表。

    代码将如下所示:

    # Postgres https://www.pulumi.com/docs/reference/pkg/postgresql/
    # provider: https://www.pulumi.com/docs/reference/pkg/postgresql/provider/
    postgres_provider = postgres.Provider("postgres-provider",
      host=myinstance.public_ip_address,
      username=users.name,
      password=users.password,
      port=5432,
      superuser=True)
    
    # creates a database on the instance in google cloud with the provider we created
    mydatabase = postgres.Database("pulumi-votes-database",
       encoding="UTF8",
       opts=pulumi.ResourceOptions(provider=postgres_provider)
    )
    
    # Table creation/deletion is via pg8000 https://github.com/tlocke/pg8000
    def tablecreation(mytable_name):
        print("tablecreation with:", mytable_name)
        create_first_part = "CREATE TABLE IF NOT EXISTS"
        create_sql_querty = "(id serial PRIMARY KEY, email VARCHAR ( 255 ) UNIQUE NOT NULL, api_key VARCHAR ( 255 ) NOT NULL)"
        create_combined = f'{create_first_part} {mytable_name}{create_sql_querty}'
        print("tablecreation create_combined_sql:", create_combined)
        myconnection=pg8000.native.Connection(
            host=postgres_sql_instance_public_ip_address,
            port=5432,
            user=postgres_sql_user_username,
            password=postgres_sql_user_password,
            database=postgres_sql_database_name
        )
    
        print("tablecreation starting")
        cursor=myconnection.run(create_combined)
        print("Table Created:", mytable_name)
        selectversion = 'SELECT version();'
        cursor2=myconnection.run(selectversion)
        print("SELECT Version:", cursor2)
    
    def droptable(table_to_drop):
        first_part_of_drop= "DROP TABLE IF EXISTS "
        last_part_of_drop= ' CASCADE'
        combinedstring = f'{first_part_of_drop} {table_to_drop} {last_part_of_drop}'
        conn=pg8000.native.Connection(
            host=postgres_sql_instance_public_ip_address,
            port=5432,
            user=postgres_sql_user_username,
            password=postgres_sql_user_password,
            database=postgres_sql_database_name
            )
        print("droptable delete_combined_sql ", combinedstring)
        cursor=conn.run(combinedstring)
        print("droptable completed ", cursor)
    

    在第一次通过pulumi up -y 启动基础架构后,您可以取消注释__main__.py 中的以下代码块,然后通过cli 添加postgressql 服务器的配置,然后运行pulumi up -y

    create_table1 = "votertable"
    creating_table = tablecreation(create_table1)
    print("")
    create_table2 = "regionals"
    creating_table = tablecreation(create_table2)
    print("")
    drop_table = "table2"
    deleting_table = droptable(drop_table)
    

    表的设置在Pulumi.dev.yaml文件中,通过pulumi config set设置

    【讨论】:

    • 天哪,正是我需要的,非常感谢!我意识到我在吹毛求疵,但你能谈谈为什么你建议评论/取消评论表创建吗?这不违背基础设施即代码的想法吗?
    • 目前,gcppostgres 中没有可供我们调用的 createtable,因此我们必须通过 pg8000 调用。资源中的 outputsOutput<T> 类型,而 pg8000 需要字符串,因此我们将需要的值作为配置传回。
    猜你喜欢
    • 2020-11-22
    • 2020-02-11
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2021-10-21
    相关资源
    最近更新 更多