Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Node image can't reach postgres when using Docker Compose locally

default discord avatar
legardus9 months ago
53

thanks a lot

  • default discord avatar
    fofoy.10 months ago

    if I think of anything else I'll let you know

  • default discord avatar
    legardus10 months ago

    thanks a lot for your help, i will try to make a post so its marked "unsolved"

  • default discord avatar
    fofoy.10 months ago

    same at my work and it works this way, I'm out of ideas for now :/

  • default discord avatar
    legardus10 months ago

    i'm running on a windows OS by the way



    it launches but same error



    you right will do

  • default discord avatar
    fofoy.10 months ago

    replace dev by gou

  • default discord avatar
    legardus10 months ago

    cant launch it, i get an error message



    ok i will try it now, where did you put it?

  • default discord avatar
    fofoy.10 months ago

    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
  • default discord avatar
    legardus10 months ago

    want me to put "postgres" instead?

  • default discord avatar
    fofoy.10 months ago

    this migth be due to the "localhost" in the

    DATABASE_URI

    ah wait

  • default discord avatar
    legardus10 months ago

    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"]
  • default discord avatar
    fofoy.10 months ago

    damn

  • default discord avatar
    legardus10 months ago

    same error



    You added "sql" and i put "postgres", so i think it fix my bad syntax but still not working

  • default discord avatar
    fofoy.10 months ago

    OK try this then :

    DATABASE_URI=postgresql://thomas:test@localhost:5432/gou_db
  • default discord avatar
    legardus10 months ago

    it launches now but same issue



    okay

  • default discord avatar
    fofoy.10 months ago

    mine for example



    remove the "-" also before the variables

  • default discord avatar
    legardus10 months ago

    it fumbles at start



    i think he doesnt like syntax



    i will try it now

  • default discord avatar
    fofoy.10 months ago
    @324573068447973376

    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
  • default discord avatar
    legardus10 months ago

    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
    
     
  • default discord avatar
    kednikolast year

    Have you tried with


    depends_on:
      - mongo
      - postgres

    ?

  • default discord avatar
    brianjmlast year

    You're welcome!

  • default discord avatar
    eloahsamlast year
    @1090615594568667286

    @221991308976324608

    Thanks for the assistance

  • default discord avatar
    brianjmlast year

    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
  • default discord avatar
    eloahsamlast year

    ohhh this would give it access to my local postgres

  • default discord avatar
    brianjmlast year

    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:
  • default discord avatar
    eloahsamlast year

    yeah i dont want to lose my data , so with docker compose you cant refer to 127.0.0.1 /localhost

  • default discord avatar
    brianjmlast year

    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.

  • default discord avatar
    eloahsamlast year

    what does restart do ?

  • default discord avatar
    brianjmlast year

    Awesome!

  • default discord avatar
    fofoy.last year

    OK great

  • default discord avatar
    eloahsamlast year

    this worked i deleted all my containers and built again and it found testdb

  • default discord avatar
    fofoy.last year

    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

  • default discord avatar
    brianjmlast year

    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?

  • default discord avatar
    eloahsamlast year

    yes it exists i can also see it in my pgadmin

  • default discord avatar
    brianjmlast year
    POSTGRES_DB
    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.
    https://hub.docker.com/_/postgres
  • default discord avatar
    fofoy.last year

    Yes that should solve this

  • default discord avatar
    brianjmlast year

    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.

  • default discord avatar
    eloahsamlast year

    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
  • default discord avatar
    brianjmlast year

    35434 is accessible from the host and externally, while 5432 is docker networking only.

  • default discord avatar
    eloahsamlast year

    let me try both

  • default discord avatar
    fofoy.last year

    True

  • default discord avatar
    brianjmlast year

    The

    DATABASE_URL

    should either be

    postgresql://postgres:postgres@127.0.0.1:35434/testdb

    or

    postgresql://postgres:postgres@postgres:5432/testdb
  • default discord avatar
    eloahsamlast year

    i get Error: cannot connect to Postgres. Details: database "testdb" does not exist

  • default discord avatar
    fofoy.last year

    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:
  • default discord avatar
    eloahsamlast year

    i think its pointing to the postgres db docker container



    but i see it in pgAdmin

  • default discord avatar
    fofoy.last year

    Yes



    Ah actually

  • default discord avatar
    eloahsamlast year

    i get Error: cannot connect to Postgres. Details: database "testdb" does not exist

  • default discord avatar
    fofoy.last year

    Ok then try replacing 127.0.0.1 with postgres

  • default discord avatar
    eloahsamlast year

    i still get the same error

  • default discord avatar
    fofoy.last year

    I think DATABASE_URI should be that : postgresql://postgres:postgres@127.0.0.1:5432/testdb

  • default discord avatar
    eloahsamlast year

    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

  • default discord avatar
    brianjmlast year

    Can you share your docker config? Where is the DB hosted?

  • default discord avatar
    fofoy.last year

    Did you change your DATABASE_URI in your

    .env

    and what did you put in there ?

  • default discord avatar
    eloahsamlast year

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.