Skip to content

Support database parameter in postgresql_server and postgresql_user_mapping resources #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bcmedeiros opened this issue Apr 22, 2025 · 2 comments

Comments

@bcmedeiros
Copy link

bcmedeiros commented Apr 22, 2025

I'm trying to create the following server resource after creating my fdw extension:

resource "postgresql_extension" "mysql_fdw" {
  name     = "mysql_fdw"
  database = postgresql_database.this.name
  schema   = "public"

  drop_cascade = var.drop_cascade
}

resource "postgresql_server" "mysql1" {
  fdw_name     = "mysql_fdw"
  server_name  = "mysql1"
  server_owner = postgresql_role.owner.name

  options = {
    host = "localhost"
    port = "3306"

    fetch_size    = 1000
    reconnect     = true
    character_set = "UTF-8"
  }

  drop_cascade = var.drop_cascade
  depends_on   = [postgresql_extension.mysql_fdw]
}

It fails with the following error:

│ Error: pq: foreign-data wrapper "mysql_fdw" does not exist
│ 
│   with module.pg1.module.stack_db.postgresql_server.mysql1,
│   on modules/database/main.tf line 130, in resource "postgresql_server" "mysql1":
│  130: resource "postgresql_server" "mysql1" {

Running the following query works, though, so this error seems to be a limitation of either this connector or the pg Go library.

CREATE SERVER mysql_main
    FOREIGN DATA WRAPPER mysql_fdw
    OPTIONS (host 'localhost', port '3306');
@bcmedeiros
Copy link
Author

bcmedeiros commented Apr 23, 2025

After some investigation, I figured out that the error occurs because I'm declaring the mysql_fdw extension to be setup in a database I'm also provisioning in this same file, using database = postgresql_database.this.name, but as the postgresql_server resource does not allow a database to be specified, the server creation is attempted in the postgres database which is the one the provider is connected to.

The workaround I'm going to have to use now is to declare a second provider with the newly created database and given that Terraform won't mix up any dependencies (I'm not even sure provider declarations wait for any dependencies) the server is created:

resource "postgresql_extension" "mysql_fdw" {
  name     = "mysql_fdw"
  database = postgresql_database.this.name
  schema   = "public"

  drop_cascade = var.drop_cascade
}

provider "postgresql" {
  alias     = "database"
  host     = var.host
  port     = var.port
  username = var.user
  password = var.pass

  database = postgresql_database.this.name

  superuser = false
  sslmode   = var.ssl ? "require" : "disable"
}

resource "postgresql_server" "mysql1" {
  provider     = postgresql.database
  server_name  = "mysql1"
  fdw_name     = "mysql_fdw"
  server_owner = postgresql_role.owner.name

  options = {
    host = var.mysql1.host
    port = var.mysql1.port

    fetch_size    = 1000
    reconnect     = true
    character_set = "UTF-8"
  }

  drop_cascade = var.drop_cascade
  depends_on   = [postgresql_extension.mysql_fdw]
}

We need a database configuration for postgresql_server as we have for postgresql_extension.

@bcmedeiros bcmedeiros changed the title Support for mysql_fdw in the postgresql_server resource Support database parameter postgresql_server resources Apr 23, 2025
@bcmedeiros bcmedeiros changed the title Support database parameter postgresql_server resources Support database parameter postgresql_server and postgresql_user_mapping resources Apr 23, 2025
@isaias-sanchez-utiq-com

Just facing this issue as well.

@bcmedeiros bcmedeiros changed the title Support database parameter postgresql_server and postgresql_user_mapping resources Support database parameter in postgresql_server and postgresql_user_mapping resources May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants