Hello,
I’ve been having a lot of trouble getting my Express app to successfully query the Postgres database. I would really appreciate any help.
I looked at the database logs and this is what happens when it’s queried:
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] Error: connect ECONNREFUSED 127.0.0.1:5432
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1602:16) {
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] errno: -111,
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] code: 'ECONNREFUSED',
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] syscall: 'connect',
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] address: '127.0.0.1',
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] port: 5432
2024-03-10T19:24:15.185 app[17816201f92558] ewr [info] }
This error happens when I go to https://express-backend.fly.dev/test in my browser. Here is the query:
const getUsers = (request, response) => {
let cn = "postgres://postgres:<connection string>@<db app name>.internal:5432/template1"
const pool = new pg.Pool({
cn,
});
pool.query("SELECT * FROM users ORDER BY id ASC", (error, results) => {
if (error) {
console.log(error)
}
response.status(200).json(results);
});
};
app.get("/test", db.getUsers);
And here is my .toml file:
app = 'express-backend'
primary_region = 'ewr'
[build]
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ['app']
[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
I also expose port 8080 in the docker file and my express app is listening as follows:
app.listen(8080, '::',() => {
console.log('Example app listening on port 8080');
});
Does anyone have any ideas as to why this isn’t working? I am also unable to query the database if I’m running the express app locally, but I get a different error that says
error: database "user" does not exist
at Parser.parseErrorMessage (/Users/user/dev/react-portfolio-site/express-backend/node_modules/pg-protocol/dist/parser.js:287:98)
at Parser.handlePacket (/Users/user/dev/react-portfolio-site/express-backend/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/Users/user/dev/react-portfolio-site/express-backend/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/Users/user/dev/react-portfolio-site/express-backend/node_modules/pg-protocol/dist/index.js:11:42)
at Socket.emit (node:events:519:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Readable.push (node:internal/streams/readable:390:5)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 97,
severity: 'FATAL',
code: '3D000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'postinit.c',
line: '887',
routine: 'InitPostgres'
}