Question

Function timeout when trying to connect to managed Postgres Database

I have DO function like below which connect to managed PostgreSQL database and call a procedure, but function timeout when I invoke it through the control panel, I even tried increasing timeout duration and memory. (tried upto 1 min, 256MB of memory)

exports.main = async function main() {
  const { Client } = require("pg");

  require("dotenv").config();

  console.log("Database URL:", process.env.DB_URL);

  const client = new Client({
    connectionString: process.env.DB_URL,
  });

  _try_ {
    _await_ client.connect();
    _await_ client.query("CALL app.prcdre()");
  } _catch_ (err) {
    console.error(err);
  } _finally_ {
    _await_ client.end();
  }
};

project.yml

packages:
  - name: sample
    functions:
      - name: dailyproc
        binary: false
        runtime: "nodejs:18"
        main: ""
        web: true
        environment:
          DATABASE_URL: "${DB_URL}"
        limits:
          timeout: 3000
          memory: 128
        triggers:
          - name: daily-payout-10pm
            sourceType: scheduler
            sourceDetails:
              cron: "00 22 * * *"

ENV is set properly as I can see with the console log. Also note this database is also attached to app platform.

How could I resolve this timeout?


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.

Bobby Iliev
Site Moderator
Site Moderator badge
July 17, 2023

Hi there,

It sounds like your function is deployed separately in a sandbox namespace rather than the App Platform. That would mean that the function is not part of the trusted sources of your database.

There are two workarounds here:

  • If possible, as a test, you can try temporarily disabling the trusted sources of your database cluster would accept connections from everywhere. This is not very recommended for security reasons.
  • Alternatively, you can deploy your function as part of the App Platform so that it would have access to the database via the allowed trusted sources, however, the downside here is that you would not have access to the beta scheduling feature.

Let me know how it goes.

Best,

Bobby

Try DigitalOcean for free

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

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

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

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

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