简体   繁体   中英

Connecting to postgres database generated from docker-compose yaml

EDIT: The docker-compose file creates a prisma instance just fine, and this can talk to the database, but I also want to be able to connect to the database from my mac on the command line as if it was a local database.

I'm using docker-compose to create a prisma api connected to a postgres container. This works fine, but I can't access the postgres instance without entering the container first, even though I thought I'd set up port forwarding. This is my docker-compose yaml:

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.13
    restart: always
    ports:
    - "4040:4040"
    environment:
      PRISMA_CONFIG: |
        port: 4040
        databases:
          default:
            connector: postgres
            host: postgres
            port: 5432
            user: prisma
            password: prisma
            migrations: true
  postgres:
    image: postgres:latest
    restart: always
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: prisma
      POSTGRES_PASSWORD: prisma
    volumes:
      - postgres:/var/lib/postgresql/data
volumes:
  postgres:

When I look at docker ps it has 0.0.0.0:5432->5432/tcp in the ports column for the postgres container, but when I try any of

psql -h localhost -p 5432 -U prisma
psql -h 0.0.0.0 -p 5432 -U prisma
psql -h 127.0.0.1 -p 5432 -U prisma

I get errors saying prisma is not a role. When I try the same thing with -U postgres I can get into the database, but the prisma database doesn't exist (but it does in the container).

I've done a lot of googling but haven't found any solution, so would be very happy if someone could explain where I'm going wrong. Thanks!

Try adding, under 'prisma', it will make sure to wait for database to startup:

depends_on:
      - postgres

Maybe your database container is not up yet when 'prisma' container trying to connect.

发布答案,使人们知道不打扰-我重新启动了postgresql的brew服务,然后它正常工作...很奇怪。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM