Skip to content
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

digitalocean_database_db: expose a specific database's connection string as output #1257

Open
bartekpacia opened this issue Oct 25, 2024 · 1 comment

Comments

@bartekpacia
Copy link

Use case

I have a Postgres database in my infrastructure. It's defined like this:

resource "digitalocean_database_cluster" "postgres" {
  name       = "bee-db-cluster-postgres"
  engine     = "pg"
  version    = "16"
  size       = "db-s-1vcpu-1gb"
  region     = "sfo2"
  node_count = 1
}

resource "digitalocean_database_db" "postgres" {
  cluster_id = digitalocean_database_cluster.postgres.id
  name       = "bee"

  provisioner "local-exec" {
    command = <<EOF
      # assumes psql is available locally
      psql -f ../backend/sql-scripts/1-schema.sql
      psql -f ../backend/sql-scripts/2-triggers.sql
      psql -f ../backend/sql-scripts/3-views.sql
      psql -f ../backend/sql-scripts/100-seed.sql
    EOF

    environment = {
      "PGUSER" = digitalocean_database_cluster.postgres.user # -U
      "PGHOST" = digitalocean_database_cluster.postgres.host # -h
      "PGPORT" = digitalocean_database_cluster.postgres.port # -p
      "PGDATABASE" = digitalocean_database_db.postgres.name  # -d
      "PGSSLMODE" = "require"                                # -c sslmode=require
      "PGPASSWORD" = digitalocean_database_cluster.postgres.password
    }
  }
}

I want to expose the connection string to my database as an output:

output "db_postgres_uri" {
  description = "Connection string for Postgres database cluster"
  value       = digitalocean_database_cluster.postgres.uri
  sensitive   = true
}

I print it like this:

$ terraform output db_postgres_uri
"postgresql://doadmin:THIS_IS_REDACTED_3NjMw@bee-db-cluster-postgres-do-user-12345678-0.l.db.ondigitalocean.com:25060/defaultdb?sslmode=require"

The problem

Notice that there's defaultdb in the output.

I want to have bee there, since this is the database I actually use.

How can I accomplish this?

@zeeket
Copy link

zeeket commented Nov 8, 2024

As a workaround, could you set the value of your output to replace(digitalocean_database_cluster.postgres.uri, defaultdb, bee)

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