5 months ago
I've been struggling for a couple days trying to get a PostgreSQL database to respond to a Node app trying to execute a simple INSERT command (create user).
When I send a POST request to the Node endpoint using Postman, I receive the following response:
"error": "connect ECONNREFUSED ::1:5432"
In the Node project, it logs the following error:
err Error: connect ECONNREFUSED ::1:5432
at /app/node_modules/pg-pool/index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async file:///app/routes/users.js:14:20 {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '::1',
port: 5432
}
The error in Node is from the following code attempting to connect to and query the database.
const response = await pool.query('INSERT INTO users (username, email, password) VALUES ($1, $2, $3) RETURNING *', [username, email, hashedPassword]);
I'm able to connect to the database and run queries using the psql connection string provided by Railway. The DATABASE_URL variable is defined in the Node project as...
DATABASE_URL="${{user-auth-db.DATABASE_URL}}"
...where the database project name in Railway is "user-auth-db". The only other environment variables are tokens related to JWT.
The only change I made to the PG database was changing the project name as indicated in the previous paragraph. All of the environment variables and settings remain unchanged from the default setup.
Finally, here is a [link to the Github repo of the Node project](https://github.com/mngatewood/user-auth).
Please let me know if I missed any required information and thanks in advance for any assistance.
4 Replies
5 months ago
The database needs to be in the same project as the app service, you can also click the eye icon on the database url variable to see that its empty, it's empty because you can't reference a service that does not exist in that project.
https://docs.railway.com/overview/best-practices#deploying-related-services-into-the-same-project
5 months ago
I did notice that it was empty, but it was visible in the raw env object, so I assumed it was good.
5 months ago
The reference syntax is visible but the rendered value is empty, for what it's worth, it is the correct syntax.