Question

Connecting a function to a managed postgresql database

Hello,

Apologies in advance for the essay, I just wanted to discuss the difficulty I faced when trying to connect a function to a managed postgresql database I created. I followed the documentation here: https://docs.digitalocean.com/products/functions/quickstart/. I think the documentation is either wrong or could be worded better, because multiple sections say I can create standalone production functions without having to create one in the Apps section/App Platform.

So the documentation indicates that I should create a new namespace and then deploy a function to that namespace. I then create a new namespace through doctl, which I can see in the Function page/portal, but it’s not associated with my Project or Apps. The documentation doesn’t mention this is an issue, so I move forward. I tested my function locally to make sure it was able to connect to the database, and I was successful after I whitelisted my IP. I then deployed my function to the namespace I created, but it was not able to connect to the database. In case it was an IP issue (the logs said it couldn’t connect), I allowed all public traffic to the database, but that still didn’t work. The reason I did this is because I couldn’t find the name of my function when searching for it in the Trusted Sources section of the database. This is most likely due to the namespace not being associated with my project from earlier…?

I then tried to create a VPC and connect the database and the function to that. I added the database but it doesn’t seem I can add a function in this page/section. One thing I noticed, which is slightly different than what I’m talking about now, is that the VPC says I have no resources attached in the Resources section, but the Overview page says I have 1 resource attached. I can confirm the database is attached to the VPC, because the Overview page of the database says it’s connected to that exact VPC. I’m not sure why the VPC doesn’t list my database then. Maybe this is a bug or the wording should be a little bit better.

I eventually create a new function through the DO portal just to make sure it’s not an issue with the CLI. I specifically do this in my project page, but the namespace and project still aren’t associated with my project. I tried reading questions asked here in the Community and some more documentation, but nothing really seemed to work. So I then try to create a new app (not a function) in the Apps section and create the resource from the code I posted to Github. This is how I eventually got the function to connect to the database.

Was there somewhere in the documentation that said I need to create a function this way in order to connect to a managed database? Because the workflow doesn’t seem intuitive or really make sense. I’m creating an App, but it’s really a function. I did eventually find this link as I was finished typing my comment/question: https://docs.digitalocean.com/products/functions/quickstart/. But, again, up until this point the Function Quickstart section and a few other places said I can just create a production ready function without having to create it in the App Platform.

Also, another random comment, I pulled down the Github example for a Python Sendgrid email function to test. I needed to add --remote-build, which was mentioned in the README, but I don’t believe I saw that mentioned anywhere in the documentation. If I don’t have this flag, trying to use the example build.sh, which uses virtualvenv and pip install the requirements, then the build doesn’t work. If that’s not listed anywhere in the documentation (even https://docs.digitalocean.com/reference/doctl/reference/serverless/deploy/#flags doesn’t explicitly mention that this flag is important), then it might be helpful to emphasis a little better or make it easier to find.

Thanks!


Submit an answer
Answer a question...

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.

Hi there! I am facing the same issue here trying to connect to a managed database via a DigitalOcean function. I’m getting “PrismaClientInitializationError: Can’t reach database server at (…)” using the same credentials that I am using locally. I don’t want to allow all public traffic for security reasons. Were you able to find a solution to your problem?