I have a question regarding using the pg
library to connect nodeJS with the Postgres database.
I'm running PostgreSQL & PgAdmin using docker, following this tutorial.
I've entered pgAdmin through http://localhost:82/
, and added PostgreSQL server which is in the host 172.17.0.2
and on port 5432
.
I found the PostgreSQL server host executing docker inspect my-own-postgres
, which retrieves 172.17.0.2
for IPAddress
.
I accessed the PostgreSQL server on the terminal and created tasksdb
database and task
table, following the code below:
docker exec -it my-own-postgres psql -U postgres
CREATE DATABASE tasksdb;
\c tasksdb
CREATE TABLE task(
id SERIAL PRIMARY KEY,
title VARCHAR(255) UNIQUE,
description VARCHAR(255)
);
Then, changes were reflected on http://localhost:82/
where pgAdmin was running. You can see from the image the PostgreSQL server connection parameters.
I'm working on a basic JS application using the PERN stack. So far, I have started the backend.
This is my index.js
file:
const express = require('express');
const morgan = require('morgan');
const taskRoutes = require('./routes/tasks.routes')
const app = express()
app.use(morgan('dev'))
app.use(taskRoutes)
app.listen(4001)
console.log('Server on port 4001')
In the routes
folder, I have the tasks.routes.js
file to list endpoints. This file looks like this, until now.
const { Router } = require('express');
const { pool } = require('../db')
const router = Router()
router.get('/tasks', async (req, res) => {
const result = await pool.query('SELECT NOW()')
console.log(result)
res.json('executed')
})
router.get('/tasks/10', (req, res) => {
res.send('retrieving a single task')
})
router.post('/tasks', (req, res) => {
res.send('creating a new task')
})
router.delete('/tasks', (req, res) => {
res.send('deleting a task')
})
router.put('/tasks', (req, res) => {
res.send('updating a task')
})
module.exports = router
A db.js
file has info for the DB connection. Here, I have used the data from the PostgreSQL server connection parameters. But after querying http://localhost:4001/tasks
, I'm getting an error.
const { Pool } = require('pg')
const pool = new Pool({
user: 'postgres',
password: 'somepass',
host: '172.17.0.2',
port: 5432,
database: 'tasksdb'
})
module.exports = pool;
This is the error I got on the terminal:
/Users/user/code/src/routes/tasks.routes.js:9
const result = await pool.query('SELECT NOW()')
^
TypeError: Cannot read properties of undefined (reading 'query')
at /Users/user/code/src/routes/tasks.routes.js:9:31
at Layer.handle [as handle_request] (/Users/user/code/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/user/code/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/Users/user/code/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/Users/user/code/node_modules/express/lib/router/layer.js:95:5)
at /Users/user/code/node_modules/express/lib/router/index.js:284:15
at Function.process_params (/Users/user/code/node_modules/express/lib/router/index.js:346:12)
at next (/Users/user/code/node_modules/express/lib/router/index.js:280:10)
at Function.handle (/Users/user/code/node_modules/express/lib/router/index.js:175:3)
at router (/Users/user/code/node_modules/express/lib/router/index.js:47:12)
So, it seems like the DB connection is not working, thus the PostgreSQL server connection parameters are wrong. Can someone help me out with how to configure this in order to use PostgreSQL & PgAdmin through docker and express in localhost:someport
?