i recently created a payload project locally and chose postgres as my db , and when i run
yarn dev
it runs successfully , then i decided to try out the docker compose and i keep getting the error
Error: cannot connect to Postgres. Details: connect ECONNREFUSED 127.0.0.1:5432
can anyone assist
Did you change your DATABASE_URI in your
.env
and what did you put in there ?
Can you share your docker config? Where is the DB hosted?
The db is local
DATABASE_URI : postgres://postgres:postgres@127.0.0.1:5432/testdb
docker-compose.yml
version: '3'
services:
payload:
image: node:18-alpine
ports:
- '3000:3000'
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
working_dir: /home/node/app/
command: sh -c "yarn install && yarn dev"
depends_on:
- mongo
env_file:
- .env
postgres:
image: postgres
hostname: localhost
ports:
# Accessible from the host at port :35432
- '35434:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
# Make sure log colors show up correctly
tty: true
mongo:
image: mongo:latest
ports:
- '27017:27017'
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
logging:
driver: none
volumes:
data:
node_modules:
FROM node:18.8-alpine as base
FROM base as builder
WORKDIR /home/node/app
COPY package*.json ./
COPY . .
RUN yarn install
RUN yarn build
FROM base as runtime
ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js
ARG DATABASE_URI
ENV DATABASE_URI=$DATABASE_URI
ARG PAYLOAD_SECRET
ENV PAYLOAD_SECRET=$PAYLOAD_SECRET
WORKDIR /home/node/app
COPY package*.json ./
COPY yarn.lock ./
RUN yarn install --production
COPY --from=builder /home/node/app/dist ./dist
COPY --from=builder /home/node/app/build ./build
EXPOSE 3000
CMD ["node", "dist/server.js"]
dockerfile
I think DATABASE_URI should be that : postgresql://postgres:postgres@127.0.0.1:5432/testdb
i still get the same error
Ok then try replacing 127.0.0.1 with postgres
i get Error: cannot connect to Postgres. Details: database "testdb" does not exist
Ah actually
Yes
but i see it in pgAdmin
i think its pointing to the postgres db docker container
version: '3'
services:
payload:
image: node:18-alpine
ports:
- '3000:3000'
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
working_dir: /home/node/app/
command: sh -c "yarn install && yarn dev"
depends_on:
- mongo
env_file:
- .env
postgres:
image: postgres
hostname: localhost
ports:
# Accessible from the host at port :35432
- '35434:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: testdb
# Make sure log colors show up correctly
tty: true
mongo:
image: mongo:latest
ports:
- '27017:27017'
command:
- --storageEngine=wiredTiger
volumes:
- data:/data/db
logging:
driver: none
volumes:
data:
node_modules:
Add
POSTGRES_DB: testdb
under postgres
i get Error: cannot connect to Postgres. Details: database "testdb" does not exist
The
DATABASE_URL
should either be
postgresql://postgres:postgres@127.0.0.1:35434/testdb
or
postgresql://postgres:postgres@postgres:5432/testdb
True
let me try both
35434 is accessible from the host and externally, while 5432 is docker networking only.
i get Error: cannot connect to Postgres. Details: connect ECONNREFUSED 127.0.0.1:35434 when i try
postgresql://postgres:postgres@127.0.0.1:35434/testdb
and i get i get Error: cannot connect to Postgres. Details: database "testdb" does not exist when i using
postgresql://postgres:postgres@postgres:5432/testdb
OK, you definately want the second one then.
Does the testdb exist?
If you started the container (witha volume) without the
POSTGRES_DB
, it probably does not.
Yes that should solve this
POSTGRES_DBhttps://hub.docker.com/_/postgres
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.
yes it exists i can also see it in my pgadmin
Does it have the correct permissions? Does a database with the same name as
POSTGRES_USER
also exist?
Perhaps you have a database called
postgres
with the correct permissions on
postgres
(user) but not the correct permissions on
testdb
?
I personally get it to work with such docker config, pretty simple
db:
image: postgres
restart: always
ports:
- '5432:5432'
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: database
I don't have a
hostname
set though
this worked i deleted all my containers and built again and it found testdb
OK great
Awesome!
what does restart do ?
You don't have a volume set up, so when you restart you will have a clean database.
That's probably not what you want.
yeah i dont want to lose my data , so with docker compose you cant refer to 127.0.0.1 /localhost
Your docker-compose for postgres should be more like this:
postgres:
image: postgres
hostname: localhost # Cannot access via 127.0.0.1 with this setting?
ports:
# Accessible from the host at port :35432
- '35434:5432'
volumes:
- pg_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: testdb
PGDATA: /var/lib/postgresql/data
# Make sure log colors show up correctly
tty: true
And add
pg_data
to
volumes
:
volumes:
data:
node_modules:
I don't think you can access via 127.0.0.1 because you specified
hostname: localhost
. I would remove that unless you feel it is necessary (doesn't seem to be).
ohhh this would give it access to my local postgres
You can also mount to a host directory, like this:
volumes:
- /your/dir:/var/lib/postgresql/data
What do you mean? If you specify
localhost
and then use
127.0.0.1
to access it, that won't work. If you want to keep
hostname: localhost
and also reference it by hostname, you can use the hostname in the env variable.
postgresql://postgres:postgres@localhost:35434/testdb
instead of
postgresql://postgres:postgres@127.0.0.1:35434/testdb
@221991308976324608
Thanks for the assistance
You're welcome!
Have you tried with
depends_on:
- mongo
- postgres
?
Hello it seems i have the same issues but i cannot resolve it with the whole thread
Here is my docker-compose
MY db is stored on docker
.env
`DATABASE_URI=postgres://thomas:test@localhost:5432/gou_db
PAYLOAD_SECRET=eecf3645a36f0175d5afdf4a
docker-compose
version: "3"
services:
payload:
image: node:18-alpine
ports:
- "3000:3000"
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
networks:
- gou
working_dir: /home/node/app/
command: sh -c "npm install && npm run dev"
depends_on:
- postgres
env_file:
- .env
postgres:
image: postgres:latest
ports:
- "5432:5432"
environment:
- POSTGRES_DB=gou_db
- POSTGRES_USER=thomas
- POSTGRES_PASSWORD=test
volumes:
- data:/var/lib/postgresql/data
networks:
- gou
volumes:
data:
node_modules:
networks:
gou:
driver: bridge
and the error sorry
I'm not sure this is the issue but can you try setting your postgres variables with ':' instead of '=' ?
This is mine for example :
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: database
i will try it now
it fumbles at start
i think he doesnt like syntax
remove the "-" also before the variables
mine for example
okay
it launches now but same issue
OK try this then :
DATABASE_URI=postgresql://thomas:test@localhost:5432/gou_db
same error
You added "sql" and i put "postgres", so i think it fix my bad syntax but still not working
damn
here is my .env
DATABASE_URI=postgresql://thomas:test@localhost:5432/gou_db
PAYLOAD_SECRET=eecf3645a36f0175d5afdf4a
my docker-compose
version: "3"
services:
payload:
image: node:18-alpine
ports:
- "3000:3000"
volumes:
- .:/home/node/app
- node_modules:/home/node/app/node_modules
networks:
- gou
working_dir: /home/node/app/
command: sh -c "npm install && npm run dev"
depends_on:
- postgres
env_file:
- .env
postgres:
image: postgres:latest
ports:
- "5432:5432"
environment:
POSTGRES_DB: gou_db
POSTGRES_USER: thomas
POSTGRES_PASSWORD: test
volumes:
- data:/var/lib/postgresql/data
networks:
- gou
volumes:
data:
node_modules:
networks:
gou:
dockerfile
FROM node:18.8-alpine as base
FROM base as builder
# Change this path to your own directory
WORKDIR C:\Users\bptho\Downloads\Gou\test
COPY package*.json ./
COPY . .
RUN npm install
RUN npm run build
FROM base as runtime
ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js
# Change this path to your own directory
WORKDIR C:\Users\bptho\Downloads\Gou\test
COPY package*.json ./
COPY package-lock.json ./
RUN npm install --production
# Change this path to your own directory
COPY --from=builder C:\Users\bptho\Downloads\Gou\test\dist ./dist
COPY --from=builder C:\Users\bptho\Downloads\Gou\test\build ./build
EXPOSE 3000
CMD ["node", "dist/server.js"]
ah wait
this migth be due to the "localhost" in the
DATABASE_URI
want me to put "postgres" instead?
for example that's mine :
DATABASE_URI=postgresql://postgres:example@cms-db:5432/database
and I had to add this in the docker-compose for it to work personally
networks:
dev:
aliases:
- cms-db
worth a try
ok i will try it now, where did you put it?
cant launch it, i get an error message
replace dev by gou
you right will do
it launches but same error
i'm running on a windows OS by the way
same at my work and it works this way, I'm out of ideas for now :/
thanks a lot for your help, i will try to make a post so its marked "unsolved"
if I think of anything else I'll let you know
thanks a lot
Star
Discord
online
Get dedicated engineering support directly from the Payload team.