I am using docker-compose to build a node express & Postgres application.
Problem
I cannot enter the container with psql to inspect the tables but my initial migration is logging that there was a successful migration.
--- EDIT --- Codebase --- END EDIT ---
Running Containers
note: the db is immediately shutting down and this might be the reason my psql is failing while the migration (which is creating a new connection) is working??
## -- LOG FROM RUNNING DB CONTAINER -- ##
2021-12-04 22:08:27.683 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2021-12-04 22:08:27.683 UTC [1] LOG: listening on IPv6 address "::", port 5432
2021-12-04 22:08:27.688 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2021-12-04 22:08:27.723 UTC [76] LOG: database system was shut down at 2021-12-04 22:08:27 UTC
2021-12-04 22:08:27.734 UTC [1] LOG: database system is ready to accept connections
ā
Successful Migration Command
docker exec reactquery_demo-backend-1 npm run migrate
ā Failed psql Command(s)
- trying to connect to db w/ docker user:
psql -p 5432 -d reactquery_demo_db -U docker - trying to connect to db container w/ docker user:
psql -p 5432 -d reactquery_demo-db-1 -U docker
THE ERROR:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "docker" does not exist
- I have seen posts about this being caused by the fact that a POSTGRES_USER is not added in the docker-compose but I have that in mine (as shown below)
- I have also tried:
- uninstalling / reinstalling / upgrading postgresql w/ homebrew
- running
rm /usr/local/var/postgres/postmaster.pidand then restarting Postgres w/ homebrew - stopping the homebrew Postgres processes completely so that is does not conflict with docker Postgres??
- when I do this I get the following error:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory Is the server running locally and accepting connections on that socket?
- when I do this I get the following error:
Repo Structure
Package.json
{
"name": "ReactQueryDemo",
"version": "0.0.1",
"description": "Simple Overview of React Query for Weekly Dev Meeting",
"main": "index.js",
"scripts": {
"dev": "node index.js",
"dev:watch": "nodemon index.js",
"migrate": "node db/scripts/migrate.js"
},
"dependencies": {
"express": "^4.17.1",
"knex": "^0.95.14",
"pg": "^8.7.1"
}
}
Docker Compose
version: '3.8'
services:
backend:
build: ./backend
ports:
- '8080:8080'
depends_on:
- db
db:
image: 'postgres'
ports:
- '5432:5432'
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=docker
- POSTGRES_DB=react_query_demo_db
volumes:
- data:/var/lib/postgresql/data
volumes:
data:
Dockerfile
FROM node:17
WORKDIR /backend
# copy everything from local backend dir
# into your current working directory in
# the container: /backend
COPY . .
# install dependencies in the container
# the .dockerignore does not copy the
# node_modules || Dockerfile itself
RUN npm i
CMD ["npm", "run", "dev"]
Backend Index
// server
const server = require("./api/server.js");
// port
const PORT = process.env.PORT || 8080;
// listening
server.listen(PORT, () => {
console.log(`*** \n\tServer listening on http://0.0.0.0:${PORT}\n***`)
});
Server
// import packages
const express = require('express')
// import routes
// create server
const server = express()
// configure server
server.use(express.json())
// configure routing
// base route
server.get('/', async(req,res) => {
console.log('React Query Demo Server Running')
res.json({ status: "SUCCESS", message: "React Query Demo Server Running" })
})
// exports
module.exports = server
Database Index
// imports
const knex = require('knex')
module.exports = knex({
client: 'postgres',
connection: {
host: 'db', // this is the name of the service in the docker-compose
user: 'docker',
password: 'password',
database: 'react_query_demo_db'
}
})

