Manage Postgres instances with Terraform¶
You can use the Snowflake Terraform provider to create, update, and
destroy Snowflake Postgres instances as infrastructure as code. The provider includes a
snowflake_postgres_instance resource for managing the full instance lifecycle.
For complete provider documentation including all available attributes and data sources, see the Terraform Registry documentation.
Note
The snowflake_postgres_instance resource is currently a preview feature in the Terraform
provider. To use it, add "snowflake_postgres_instance_resource" to the
preview_features_enabled list in your provider configuration. The resource schema may change
in future provider releases. This doesn’t affect the Snowflake Postgres feature itself, which
is generally available.
Provider configuration¶
Configure the Snowflake provider with preview_features_enabled to use the Postgres instance
resource:
Create an instance¶
The following example creates a minimal Postgres instance:
Postgres instance operations can take several minutes. Always include a timeouts block with
values of at least 10 minutes for create and delete. For instances with high availability
enabled, you may need even more time.
Create an instance with a network policy¶
A common workflow is to create a network rule and network policy in the same Terraform configuration, then reference the policy in the Postgres instance. Terraform handles the dependency ordering automatically.
Postgres network rules require type = "IPV4" with mode = "POSTGRES_INGRESS":
Caution
Don’t use type = "HOST_PORT" with mode = "INGRESS" for Postgres network rules. Postgres
ingress requires type = "IPV4" with mode = "POSTGRES_INGRESS".
Get connection details¶
After creating an instance, you can retrieve the connection hostname from the resource’s computed attributes:
The port is always 5432 unless you’re using connection pooling, which uses port 6432. Credentials aren’t available through Terraform and must be configured separately:
- For instances with
authentication_authority = "POSTGRES": reset credentials through Snowsight or the Snowflake CLI. - For instances with
authentication_authority = "POSTGRES_OR_SNOWFLAKE": generate an access token using GENERATE_POSTGRES_ACCESS_TOKEN_FOR_USER after configuring a role mapping.
For more information about connecting, see Connecting to Snowflake Postgres.
Async operations¶
Some changes to Postgres instances, including compute family changes, version upgrades, and enabling high availability, complete asynchronously after Terraform reports success. The instance remains available during these operations.
To check whether a background operation has completed, run:
When the operations field is empty ({}), all background operations have finished.
Note
Running terraform plan immediately after an apply that triggered an async operation won’t
show drift, even though the change hasn’t completed in Snowflake yet. If your pipeline
depends on the change being live, add a verification step outside Terraform.
Import an existing instance¶
To bring an existing Postgres instance under Terraform management:
The name must be wrapped in single quotes containing double quotes (the outer quotes are for the shell, the inner quotes are the Snowflake identifier format).
Limitations¶
- Credentials: there’s no way to set or retrieve Postgres credentials through Terraform. Credential setup requires a separate step in Snowsight or SQL.
- Scope: not all Snowflake Postgres features are configurable through Terraform. See Managing instances for additional features for managing Postgres instances.