Database Connect to Postgre works fine in development but times out in deployed

QUESTION
I have an API app that’s created with nodejs and express. One of the endpoints reads and writes from a PostgreSQL database. When in development environment it works perfectly fine. When I take the same code and deploy it (even to a brand new deployment) it won’t connect to the database. I’ve debugged it to the point where it’s timing out when trying to connect. I’m using the pg library to connect.

Below is a code snippet. It never makes it past pool.connect() on the deployed machine but has no problem in the development environment with the same code.

const { Pool } = require(‘pg’)
const pool = new Pool({
  user: process.env.PG_USER,
  host: process.env.PGHOST,
  database: process.env.PGDATABASE,
  password: process.env.PGPASSWORD,
  port: process.env.PORT,
  ssl: {
    rejectUnauthorized: false
  }
});

pool.connect()

I should mention that I’m using autoscale deployment, in case that matters.

1 Like

Welcome to the forums, @mattajohnsondev!
Your PostgreSQL database will disconnect from your program after 5 minutes.

1 Like

I resolved this by changing how I connect to the database. I just did the following and it worked fine in the production/deployed environment:

const pool = new Pool({
  connectionString: process.env.DATABASE_URL
});
pool.connect()

I don’t know why I needed to do that as opposed to the other method, but oh well…

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.