Skip to content

postgresql_user_mapping not working for public #306

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
kudryk opened this issue May 15, 2023 · 2 comments
Open

postgresql_user_mapping not working for public #306

kudryk opened this issue May 15, 2023 · 2 comments

Comments

@kudryk
Copy link

kudryk commented May 15, 2023

Hi there,

Thank you for opening an issue. Please provide the following information:

Terraform Version

  • docker image hashicorp/terraform:1.4
  • Installing cyrilgdn/postgresql v1.19.0...
  • Installed cyrilgdn/postgresql v1.19.0
  • Installed hashicorp/random v3.5.1 (signed by HashiCorp)
  • Installed hashicorp/aws v4.67.0 (signed by HashiCorp)

Affected Resource(s)

Please list the resources as a list, for example:

  • postgresql_user_mapping

I have successful created a remote server using terraform and have mapped 2 local users to remote users using postgresql_user_mapping. However, I am unable to map the local public user to a remote user.

If I map the public user in SQL succeeds but using postgres_user_mapping fails.

-- SQL: succeeds
CREATE USER MAPPING FOR PUBLIC SERVER ep_modules_db_staging OPTIONS (user 'tsds_read_only', password 'XYZ')

-- Terraform: fails
resource "postgresql_user_mapping" "staging_public" {
  server_name = postgresql_server.remote_staging_server.server_name
  user_name   = var.lab_public_username             # "public"
  options = {
    user = var.staging_readonly_username            # tsds_read_only
    password = var.staging_readonly_password   # XYZ
  }
}

When it fails, it's because it's trying to find a public role. It appears not to recognize public as a keyword for the underlying SQL command.

Plan: 1 to add, 0 to change, 0 to destroy.
postgresql_user_mapping.staging_public: Creating...
╷
│ Error: Could not create user mapping: pq: role "PUBLIC" does not exist
│ 
│   with postgresql_user_mapping.staging_public,
│   on fdw.tf line 43, in resource "postgresql_user_mapping" "staging_public":
│   43: resource "postgresql_user_mapping" "staging_public" {
@kinghuang
Copy link

kinghuang commented May 24, 2023

I think what's happening is that the code is trying to get a role named public instead of just passing the value of the user_name attribute. This works for real roles, but not public.

username := d.Get(userMappingUserNameAttr).(string)
serverName := d.Get(userMappingServerNameAttr).(string)
b := bytes.NewBufferString("CREATE USER MAPPING ")
fmt.Fprint(b, " FOR ", pq.QuoteIdentifier(username))
fmt.Fprint(b, " SERVER ", pq.QuoteIdentifier(serverName))

@seboudry
Copy link

I created a PR to resolve this issue #515

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

3 participants