thanks a lot
if I think of anything else I'll let you know
thanks a lot for your help, i will try to make a post so its marked "unsolved"
same at my work and it works this way, I'm out of ideas for now :/
i'm running on a windows OS by the way
it launches but same error
you right will do
replace dev by gou
cant launch it, i get an error message
ok i will try it now, where did you put it?
worth a try
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
want me to put "postgres" instead?
this migth be due to the "localhost" in the
DATABASE_URI
ah wait
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"]
damn
same error
You added "sql" and i put "postgres", so i think it fix my bad syntax but still not working
OK try this then :
DATABASE_URI=postgresql://thomas:test@localhost:5432/gou_db
it launches now but same issue
okay
mine for example
remove the "-" also before the variables
it fumbles at start
i think he doesnt like syntax
i will try it now
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
and the error sorry
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
Have you tried with
depends_on:
- mongo
- postgres
?
You're welcome!
@221991308976324608
Thanks for the assistance
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
You can also mount to a host directory, like this:
volumes:
- /your/dir:/var/lib/postgresql/data
ohhh this would give it access to my local postgres
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).
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:
yeah i dont want to lose my data , so with docker compose you cant refer to 127.0.0.1 /localhost
That's probably not what you want.
You don't have a volume set up, so when you restart you will have a clean database.
what does restart do ?
Awesome!
OK great
this worked i deleted all my containers and built again and it found 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
Perhaps you have a database called
postgres
with the correct permissions on
postgres
(user) but not the correct permissions on
testdb
?
Does it have the correct permissions? Does a database with the same name as
POSTGRES_USER
also exist?
yes it exists i can also see it in my pgadmin
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 that should solve this
If you started the container (witha volume) without the
POSTGRES_DB
, it probably does not.
Does the testdb exist?
OK, you definately want the second one then.
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
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
35434 is accessible from the host and externally, while 5432 is docker networking only.
let me try both
True
The
DATABASE_URL
should either be
postgresql://postgres:postgres@127.0.0.1:35434/testdb
or
postgresql://postgres:postgres@postgres:5432/testdb
i get Error: cannot connect to Postgres. Details: database "testdb" does not exist
Add
POSTGRES_DB: testdb
under postgres
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:
i think its pointing to the postgres db docker container
but i see it in pgAdmin
Yes
Ah actually
i get Error: cannot connect to Postgres. Details: database "testdb" does not exist
Ok then try replacing 127.0.0.1 with postgres
i still get the same error
I think DATABASE_URI should be that : postgresql://postgres:postgres@127.0.0.1:5432/testdb
dockerfile
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"]
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:
docker-compose.yml
DATABASE_URI : postgres://postgres:postgres@127.0.0.1:5432/testdb
The db is local
Can you share your docker config? Where is the DB hosted?
Did you change your DATABASE_URI in your
.env
and what did you put in there ?
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
Star
Discord
online
Get dedicated engineering support directly from the Payload team.