如何使用 docker-compose 连接到 PostgreSQL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50983177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:28:56  来源:igfitidea点击:

How to connect to PostgreSQL using docker-compose?

postgresqldockergoservicedocker-compose

提问by v v

Want to use docker-composeto run api application and postgresql database together.

想用来docker-compose一起运行 api 应用程序和 postgresql 数据库。

docker-composefile:

docker-compose文件:

version: '3'

volumes:
  database_data:
    driver: local

services:
  db:
    image: postgres:latest
    volumes:
      - database_data:/var/lib/postgresql/data

  api:
    build: ./api
    expose:
      - 8080
    ports:
      - 8080:8080
    volumes:
      - ./api:/usr/src/app/
    links:
      - db
    environment:
      - PGHOST=db
      - PGDATABASE=postgres
      - PGUSER=postgres

Api main.gofile:

apimain.go文件:

func main() {
    db, err = gorm.Open("postgres", "host=db port=5432 user=postgres dbname=postgres")
  // ...
}

When run the services, got message from log:

运行服务时,从日志中得到消息:

api_1     | [GIN] 2018/06/22 - 07:31:10 | 404 |      1.4404ms |      172.20.0.1 | GET      /posts
api_1     |
api_1     | (sql: database is closed)
api_1     | [2018-06-22 07:31:10]
api_1     |
api_1     | (sql: database is closed)
api_1     | [2018-06-22 07:31:10]
api_1     | [GIN] 2018/06/22 - 07:32:14 | 403 |        15.6μs |      172.20.0.1 | GET      /posts
db_1      | 2018-06-22 07:34:27.296 UTC [81] FATAL:  role "root" does not exist
db_1      | 2018-06-22 07:34:36.897 UTC [90] FATAL:  role "root" does not exist

Does this way not good? host=dbin the connection string? Since dbis the docker compose service name.

这种方式不好吗?host=db在连接字符串中?因为db是 docker compose 服务名称。



Add

添加

It can work:

它可以工作:

https://docs.docker.com/samples/library/postgres/#-or-via-psql

https://docs.docker.com/samples/library/postgres/#-or-via-psql

回答by agusgambina

In the linkyou provided there are configuration settings that you did not added

在您提供的链接中,有您未添加的配置设置

restart: always
environment:
  POSTGRES_PASSWORD: example

You should try this

你应该试试这个

version: '3'

services:
  db:
    image: postgres:latest
    restart: always
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: 'postgres'
    volumes:
      - database_data:/var/lib/postgresql/data

  api:
    build: ./api
    expose:
      - 8080
    ports:
      - 8080:8080
    volumes:
      - ./api:/usr/src/app/
    links:
      - db
    environment:
      - PGHOST: 'db'
      - PGDATABASE: 'postgres'
      - PGUSER: 'postgres'
      - PGPASSWORD: 'postgres'


volumes:
  database_data:
    driver: local

and

db, err := gorm.Open("postgres", "host=db port=5432 user=postgres dbname=postgres password=postgres")