如何使用 Pulumi 将 PostgreSQL 架构部署到 Google Cloud SQL 数据库?

问题描述

我正在尝试使用 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,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,)

解决方法

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

代码如下:

{
MessageResponse: {
ApplicationId: "myApplicationId",RequestId: "8beaebf0-f33a-42d3-bb88-1f3efcf493eb",Result: {
+14325948991: {
DeliveryStatus: "PERMANENT_FAILURE",StatusCode: 404,StatusMessage: "Resource not found"
}
}
}
}

第一次通过 # 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,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,database=postgres_sql_database_name ) print("droptable delete_combined_sql ",combinedstring) cursor=conn.run(combinedstring) print("droptable completed ",cursor) 启动基础架构后,您可以取消注释 pulumi up -y 中的以下代码块,然后通过 cli 添加 postgressql 服务器的配置,然后运行 ​​{{1} }

__main__.py

表的设置在 pulumi up -y 文件中,并通过 pulumi config set

设置