Question

Bindable environment variable not working when using Postgres connection pool on App Platform

I have a Django app running on the app platform. I want to switch over to use connection pooling.

  • I have an existing database my_db.
  • I’ve set up a Postgres connection pool my_pool.
  • I’ve detached and reattached the postgres component to my app, configured with the new connection pool

Additionally, you can use these same variable names to refer to your database connection pool (PostgreSQL only). To do so, use the syntax ${<database component name>.<connection pool name>.VARIABLE_NAME}. For example, to create a bindable variable for a pool’s connection string, you can use ${mydb.mypool.DATABASE_URL}.

So now my (abbreviated) appspec now looks like:

databases:
- cluster_name: postgres
  db_name: my_db
  db_user: my_db
  engine: PG
  name: postgres
  production: true
  version: "13"
  
# ...

envs:
- key: DATABASE_URL
  scope: RUN_AND_BUILD_TIME
  value: ${postgres.my_pool.DATABASE_URL}

This flat out doesn’t work. The error from the build is:

07:24] django.core.exceptions.ImproperlyConfigured: Set the {postgres.my_pool.DATABASE_URL}

Which would indicate that the environment variable hasn’t been set correctly. Why doesn’t this format work?

Show comments

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

I’m also having this issue. Has anyone had success resolving this?

Bobby Iliev
Site Moderator
Site Moderator badge
May 25, 2023

Hi there,

I’ve tested this with the following app spec file and it seems to be working as expected:

alerts:
- rule: DEPLOYMENT_FAILED
- rule: DOMAIN_FAILED
databases:
- cluster_name: db-postgresql-fra1-1234
  db_name: defaultdb
  db_user: doadmin
  engine: PG
  name: db-postgresql-fra1-1234
  production: true
  version: "15"
ingress:
  rules:
  - component:
      name: sample-nodejs
    match:
      path:
        prefix: /
name: sample-nodejs
region: fra
services:
- environment_slug: node-js
  envs:
  - key: DATABASE_URL
    scope: RUN_TIME
    value: ${db-postgresql-fra1-1234.your-pool.DATABASE_URL}
  git:
    branch: main
    repo_clone_url: https://github.com/digitalocean/sample-nodejs.git
  http_port: 8080
  instance_count: 1
  instance_size_slug: basic
  name: sample-nodejs

When you attached the database to your app, did you also select the pool?

Also as a side note, database values are not available during build time but are available at runtime.

Best,

Bobby

To follow up, if I set two environment variables:

envs:
- key: DATABASE_URL
  scope: RUN_AND_BUILD_TIME
  value: ${postgres.DATABASE_URL}
- key: DATABASE_POOL_URL
  scope: RUN_AND_BUILD_TIME
  value: ${postgres.my_pool.DATABASE_URL}

After deployment, echoing the two variables via the console confirms that ${postgres.my_pool.DATABASE_URL} format doesn’t work:

/app # echo $DATABASE_POOL_URL
${postgres.my_pool.DATABASE_URL}

The variable isn’t being replaced, it’s just being set as the literal string ${postgres.my_pool.DATABASE_URL}. In other words, variable binding isn’t working.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

card icon
Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Sign up
card icon
Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

Learn more
card icon
Become a contributor

You get paid; we donate to tech nonprofits.

Learn more
Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

Learn more ->
DigitalOcean Cloud Control Panel
Get started for free

Enter your email to get $200 in credit for your first 60 days with DigitalOcean.

New accounts only. By submitting your email you agree to our Privacy Policy.

© 2023 DigitalOcean, LLC.