4 months ago
No matter how much I try I am unable to make my node JS backend connect to PostgreSQL DB running within Railway.
Created a new variable DATABASE_URL in the backend service. Assigned it the following value ${{ Postgres.DATABASE_URL }}
Using the variable in the application code
Keep getting the following error. Any help is greatly appreciated.
Health check: http://0.0.0.0:8080/api/health
2025-08-11T04:28:39.154Z [ERROR] Error connecting to the database: {"code":"ECONNREFUSED","stack":"AggregateError [ECONNREFUSED]: \n at internalConnectMultiple (node:net:1134:18)\n at afterConnectMultiple (node:net:1715:7)"}
2025-08-11T04:29:28.865Z [ERROR] Error fetching merchants: {"code":"ECONNREFUSED","stack":"AggregateError [ECONNREFUSED]: \n at /app/node_modules/pg-pool/index.js:45:11\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async /app/index.js:239:20"}
20 Replies
4 months ago
Hey there! We've found the following might help you get unblocked faster:
🧵 PostgreSQL SSL connection: "self-signed certificate in certificate chain" on Node.js
🧵 ParadeDB PostgreSQL Container Fails to Initialize Due to Volume Mount Issues
If you find the answer from one of these, please let us know by solving the thread!
4 months ago
With Pro account it looks like I have priority support. This issue is still not resolved. Can I get support from the support team?
4 months ago
The ECONNREFUSED error almost always happens when your application tries to connect to the database using its public URL from within Railway's network. For services to communicate with each other inside a project, you must use the private connection URL.
## The Cause: Public vs. Private Networking
Railway provides two separate connection strings for your PostgreSQL database:
DATABASE_URL: This is the public URL, meant for connecting to your database from outside Railway (like from your local computer).DATABASE_PRIVATE_URL: This is the private URL, designed for fast and secure communication between services running inside the same Railway project.
When your backend service uses the public DATABASE_URL, it tries to exit Railway's private network to the public internet and then connect back in. This path is often blocked or not configured correctly, leading to the "Connection Refused" error.
It's like trying to mail a letter to your next-door neighbor by putting it in a public mailbox across town, instead of just walking over and handing it to them.
## How to Fix It
The solution is to change the variable reference in your Node.js service to use the private connection string.
In your Railway project, go to your Node.js backend service.
Click on the Variables tab.
Find the
DATABASE_URLvariable you created.Change its value from
${{ Postgres.DATABASE_URL }}to${{ Postgres.DATABASE_PRIVATE_URL }}.
After making this change, redeploy your backend service. The new environment variable will be injected, and your application should be able to connect to the database instantly over the private network.
4 months ago
Thanks for getting back to me. I followed the four recommended steps, but unfortunately, the issue still persists. Please see the attached screenshots for reference.
The DATABASE_URL is actually pointing to the internal domain not to the external domain, as shown in the second screenshot. To rule out variable reference issues, I even hardcoded the actual internal URL directly into the backend service, but that did not resolve the problem either.
The Node.js app log is now showing a slightly different error:
Error connecting to the database: getaddrinfo ENOTFOUND postgres.railway.internal
However, it still fails to connect.
2025-08-12T13:11:51.452Z [INFO] Health check: http://0.0.0.0:3001/api/health
2025-08-12T13:11:51.619Z [ERROR] Error connecting to the database: getaddrinfo ENOTFOUND postgres.railway.internal {"errno":-3008,"code":"ENOTFOUND","syscall":"getaddrinfo","hostname":"postgres.railway.internal","stack":"Error: getaddrinfo ENOTFOUND postgres.railway.internal\n at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:122:26)"}
Attachments
4 months ago
I am stuck on this. Any quick response is greatly appreciated.
4 months ago
Can you please provide the server file, if it's okay!
4 months ago
@mani-duraisamy - you've shared way too much of your postgreSQL password. If this is a new app, I'd recommend resetting the password, or starting over, after you find a fix for your issue.
clashing
Can you please provide the server file, if it's okay!
4 months ago
Sorry! What specific server file you are looking for?
mani-duraisamy
Sorry! What specific server file you are looking for?
4 months ago
In which you have defined your endpoints!! (/api/health, and others)
clashing
In which you have defined your endpoints!! (/api/health, and others)
4 months ago
Please download the attached file and rename it to a zip file and open.
mani-duraisamy
Please download the attached file and rename it to a zip file and open.
4 months ago
We can't open txt files!!
You just have to copy and paste the file contents in this discussion so that we can have a look at that! (As simple as that)
4 months ago
There is a 5000 char limit on the content that I can paste here. That is why I was trying to upload is a file.
I made the repo public for now. please take a look
https://github.com/mani-duraisamy/HirePros-Backend/
clashing
We can't open txt files!! You just have to copy and paste the file contents in this discussion so that we can have a look at that! (As simple as that)
4 months ago
There is a 5000 char limit on the content that I can paste here. That is why I was trying to upload is a file.
I made the repo public for now. please take a look
https://github.com/mani-duraisamy/HirePros-Backend/
4 months ago
I was just asking for the index.js file, & that also the snippet of its initial config lines. But no worries, I have seen the same via your GitHub repo.
It appears to be a simple DB connectivity issue. Could you please visit the deployed Postgres service and verify whether it is up and running? There you would find the connetion string also, you have to use that as your env variable for the backend service. And make sure that the env variables are correctly pointing to that deployed PG instance.
In the deployed instance of the DB, you can see that the variables tab is showing all of the valid key-value pairs.
Instead of referencing and creating a lot of environment variables in your backend deployment, you can use Postgres.DATABASE_URL, which uses the private network, and saves you the egress costs.
Go to your server's deployment, and type in DATABASE_URL in the variables panel:
And then choose the same from the dropdown (make sure to use the one without the PUBLIC stuff)
And now you can reference the DATABASE_URL in your pool config, instead of providing the individual connection parts like PGUSER, etc..
Change this
const pool = new Pool({
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
password: process.env.PGPASSWORD,
port: process.env.PGPORT,
});
to
const pool = new Pool({
connectionString: process.env.DATABASE_URL
});
This would help you out for sure. (Make sure to provide valid values for other env variables like process.env.R2_ENDPOINT, and others)
4 months ago
Thanks for your detailed response. I forgot to mention—please refer to the dev branch in the repository I shared earlier.
My Node.js backend can connect successfully to the Railway Postgres database when using Postgres.DATABASE_PUBLIC_URL, but fails when using the internal DATABASE_URL. I’d like to use the internal connection to avoid egress costs, but it consistently results in a DNS resolution error.
Environment Setup:
Postgres.DATABASE_URL– used only for Railway internal connection.PGUSER,PGHOST,PGDATABASE,PGPORT,PGPASSWORD– used only for local development.Postgres service is confirmed to be running at all times.
Node.js backend connects and APIs function as expected with the
Postgres.DATABASE_PUBLIC_URL
Steps Taken:
Hardcoded the internal URL directly in the backend variable
DATABASE_URL:postgresql://postgres:xxxxxxxxxxxxxxxxxxxx@postgres.railway.internal:5432/railwayRedeployed the backend to ensure changes were applied.
Current Error (when using internal DATABASE_URL):
Error connecting to the database: getaddrinfo ENOTFOUND postgres.railway.internal
Request:
Please advise if there are any additional configuration steps or Railway-side network settings required for the internal hostname to resolve properly from the Node.js service.
From the log entries...
2025-08-14T13:13:35.789Z [INFO] === SERVER STARTUP COMPLETE ===
=== DATABASE CONNECTION ERROR ===
Error connecting to database: getaddrinfo ENOTFOUND postgres.railway.internal
Error code: ENOTFOUND
Error errno: -3008
Error syscall: getaddrinfo
Error hostname: postgres.railway.internal
Full error object: {
"errno": -3008,
"code": "ENOTFOUND",
"syscall": "getaddrinfo",
"hostname": "postgres.railway.internal"
}
=== END DATABASE CONNECTION ERROR ===
4 months ago
Can this be escalated for a faster response?
mani-duraisamy
Thanks for your detailed response. I forgot to mention—please refer to the dev branch in the repository I shared earlier.My Node.js backend can connect successfully to the Railway Postgres database when using Postgres.DATABASE_PUBLIC_URL, but fails when using the internal DATABASE_URL. I’d like to use the internal connection to avoid egress costs, but it consistently results in a DNS resolution error.Environment Setup:Postgres.DATABASE_URL – used only for Railway internal connection.PGUSER, PGHOST, PGDATABASE, PGPORT, PGPASSWORD – used only for local development.Postgres service is confirmed to be running at all times.Node.js backend connects and APIs function as expected with the Postgres.DATABASE_PUBLIC_URLSteps Taken:Hardcoded the internal URL directly in the backend variable DATABASE_URL:postgresql://postgres:xxxxxxxxxxxxxxxxxxxx@postgres.railway.internal:5432/railwayRedeployed the backend to ensure changes were applied.Current Error (when using internal DATABASE_URL):Error connecting to the database: getaddrinfo ENOTFOUND postgres.railway.internalRequest:Please advise if there are any additional configuration steps or Railway-side network settings required for the internal hostname to resolve properly from the Node.js service.From the log entries...2025-08-14T13:13:35.789Z [INFO] === SERVER STARTUP COMPLETE ====== DATABASE CONNECTION ERROR ===Error connecting to database: getaddrinfo ENOTFOUND postgres.railway.internalError code: ENOTFOUNDError errno: -3008Error syscall: getaddrinfoError hostname: postgres.railway.internalFull error object: {"errno": -3008,"code": "ENOTFOUND","syscall": "getaddrinfo","hostname": "postgres.railway.internal"}=== END DATABASE CONNECTION ERROR ===
4 months ago
Hi mani, it's great to see that the public pg URL is working for you. Using the internal URL with another Railway application is very much the same!I just created a simple PG Express server and tested it with both the public and Internal (private) PG endpoints, and both worked successfully. The ENOTFOUND error occurs when you attempt to access the internal PG URL from your local machine or another Railway project. The service in which you are trying to use the internal URL of the database needs to be in the same Railway environment/project, and then only the internal calls would be successful!
So, if with the public domain, your localhost code is working fine, it would work fine with the internal URL also (just keep the fact in mind that it would work when deployed in RAILWAY environment only, in which the PG instance is also running).
While testing on localhost, you can use the public endpoint, and when done with your changes, replace the public one with the internal URL before pushing it to the Railway environment.
I hope that clears all of your doubts
Attachments
clashing
Hi mani, it's great to see that the public pg URL is working for you. Using the internal URL with another Railway application is very much the same!I just created a simple PG Express server and tested it with both the public and Internal (private) PG endpoints, and both worked successfully. The ENOTFOUND error occurs when you attempt to access the internal PG URL from your local machine or another Railway project. The service in which you are trying to use the internal URL of the database needs to be in the same Railway environment/project, and then only the internal calls would be successful!So, if with the public domain, your localhost code is working fine, it would work fine with the internal URL also (just keep the fact in mind that it would work when deployed in RAILWAY environment only, in which the PG instance is also running).While testing on localhost, you can use the public endpoint, and when done with your changes, replace the public one with the internal URL before pushing it to the Railway environment. I hope that clears all of your doubts
4 months ago
Turns out I had created the PostgreSQL service and my Node.js backend as two separate projects instead of adding them as modules within the same project. I hadn’t noticed the ‘Create’ button in the top-right corner that allows adding multiple modules under one project. Once I corrected that, I was able to connect to the internal DB URL without any issues. Thanks for all your help! Much Appreciated.
clashing
Hi mani, it's great to see that the public pg URL is working for you. Using the internal URL with another Railway application is very much the same!I just created a simple PG Express server and tested it with both the public and Internal (private) PG endpoints, and both worked successfully. The ENOTFOUND error occurs when you attempt to access the internal PG URL from your local machine or another Railway project. The service in which you are trying to use the internal URL of the database needs to be in the same Railway environment/project, and then only the internal calls would be successful!So, if with the public domain, your localhost code is working fine, it would work fine with the internal URL also (just keep the fact in mind that it would work when deployed in RAILWAY environment only, in which the PG instance is also running).While testing on localhost, you can use the public endpoint, and when done with your changes, replace the public one with the internal URL before pushing it to the Railway environment. I hope that clears all of your doubts
4 months ago
Great! You can also right-click on the canvas to add mutliple services to the same project.
clashing
Hi mani, it's great to see that the public pg URL is working for you. Using the internal URL with another Railway application is very much the same!I just created a simple PG Express server and tested it with both the public and Internal (private) PG endpoints, and both worked successfully. The ENOTFOUND error occurs when you attempt to access the internal PG URL from your local machine or another Railway project. The service in which you are trying to use the internal URL of the database needs to be in the same Railway environment/project, and then only the internal calls would be successful!So, if with the public domain, your localhost code is working fine, it would work fine with the internal URL also (just keep the fact in mind that it would work when deployed in RAILWAY environment only, in which the PG instance is also running).While testing on localhost, you can use the public endpoint, and when done with your changes, replace the public one with the internal URL before pushing it to the Railway environment. I hope that clears all of your doubts
4 months ago
There would be a button labelled as Mark this as the solution.Click on that button (below the post that helped you)

