Connecting to Managed Database with Functions in App Platform

Hello, I’m fairly new to Digital Ocean and Managed Databases and I’ve been having some trouble getting things wired up.

I created an App using the App Platform and have a Managed Database and a handful of Functions in it. I am trying to get one of the Functions to write data to the Managed Database and have been unsuccessful so far. Can anyone provide some steps or documentation on this? Specifically, I am confused about what environment variables are available at runtime and how to use them to connect to the databases within my Managed Database.

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.

Jon Friesen
DigitalOcean Employee
DigitalOcean Employee badge
August 9, 2022

👋 @mattalco

Sorry to hear you’re having trouble. We have some documentation on managing databases, though it sounds like you may be past that.

With regard to variables, bind variables can be used to inject credentials for connecting to a database. For example, if you have a database resource called “my-db” you could add an environment variable to your app (or a sub component) with the value:

Key: DB_URL Value: ${my-db.DATABASE_URL}

From within your app the environment variable $DB_URL will contain the connection string for your database. Pass that to your DB client of choice and you should be able to read and write.

Try DigitalOcean for free

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

Sign up