Report this

What is the reason for this report?

Can't connect from my K8s Cluster to my Managed Database

Posted on March 13, 2025
aq

By aq

Hi,

I am currently running a managed postgres database and a kubernetes cluster in digital ocean.

These are both running in the same VPC

I have added the kubernetes cluster to the trusted sources of the managed database.

I am able to connect to the database from my local machine if I add my local to my trusted sources.

However, I am seeing that my connection keeps timing out when trying to connect to the database in one of my k8s pods.

My code looks like this:

import dotenv from 'dotenv';
import pg from 'pg';

dotenv._config_();

export const _pool_ = new pg._Pool_({
  connectionString: _process_.env.DATABASE_URL,
  ssl:
    _process_.env.ENVIRONMENT !== 'local'
      ? {
          rejectUnauthorized: true,
          ca: _process_.env.DATABASE_CRT,
        }      : undefined,
});

I pass the DATABASE_CRT and the DATABASE_URL via secrets. I tried base64 encoded and decoding the DATABASE_CRT but I was still unable to connect. I also created a small script that I ran in the pod with the DATABASE_CRT in-lined, also was timing out.

I am using the VPC connection string as the DATABASE_URL, and the ca-certificate downloaded from the managed postgres page.



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!

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.

Hey there 👋

Just a quick note: I noticed there’s a duplicate of this same question in the forum, so I’ll go ahead and remove the duplicate to keep things tidy:

👉 https://www.digitalocean.com/community/questions/can-t-connect-from-my-k8s-cluster-to-my-managed-database-5ded7113-5c3f-4676-bc67-e74cc833f5fb?comment=210354

- Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.