简体   繁体   English

连接到从docker-compose yaml生成的postgres数据库

[英]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. 编辑:docker-compose文件可以很好地创建一个prisma实例,它可以与数据库通信,但是我也希望能够在命令行上从我的mac连接到数据库,就好像它是本地数据库一样。

I'm using docker-compose to create a prisma api connected to a postgres container. 我正在使用docker-compose创建连接到postgres容器的pyramida api。 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. 这可以正常工作,但是即使我认为我将设置端口转发,也必须先进入容器才能访问postgres实例。 This is my docker-compose yaml: 这是我的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 当我查看docker ps它在postgres容器的端口栏中有0.0.0.0:5432->5432/tcp ,但是当我尝试任何

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. 我收到错误消息,指出prisma不是角色。 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). 当我使用-U postgres尝试相同的操作时,我可以进入数据库,但是不存在pyramida数据库(但是它在容器中存在)。

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: 尝试在“ prisma”下添加,它将确保等待数据库启动:

depends_on:
      - postgres

Maybe your database container is not up yet when 'prisma' container trying to connect. 当“ prisma”容器尝试连接时,您的数据库容器可能尚未启动。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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