postgresql 尝试执行“docker up”时出现“致命:角色“root”不存在”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37970738/
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-10-21 02:19:04  来源:igfitidea点击:

Getting "FATAL: role "root" does not exist" when trying to do "docker up"

ruby-on-railspostgresqldocker

提问by Jason Swett

I'm starting to work with an existing Rails project that uses Docker. I've used Rails for a long time but never Docker.

我开始使用现有的使用 Docker 的 Rails 项目。我已经使用 Rails 很长时间了,但从未使用过 Docker。

After I do a docker build .I try to do a docker-compose up, but I get:

在我做 a 之后,docker build .我尝试做 a docker-compose up,但我得到:

FATAL: role "root" does not exist /usr/local/bundle/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect': FATAL: role "root" does not exist (ActiveRecord::NoDatabaseError)

致命:角色“root”不存在 /usr/local/bundle/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect':致命:角色“root”不存在不存在 (ActiveRecord::NoDatabaseError)

It seems to me that the Docker machine is probably trying to connect to the database as the rootuser, but there's no role called root, so the connection is rightly failing.

在我看来,Docker 机器可能正在尝试以root用户身份连接到数据库,但没有名为 的角色root,因此连接失败是正确的。

The thing I don't know is why Docker is apparently trying to connect to the database as rootand how to get it to use the right user.

我不知道的是为什么 Docker 显然试图连接到数据库root以及如何让它使用正确的用户。

Here's my database.yml:

这是我的database.yml

development:
  database: my_app_development
  adapter: postgresql
  encoding: unicode
  pool: 5

Any help is appreciated.

任何帮助表示赞赏。

Edit: here's my docker-compose.yml:

编辑:这是我的docker-compose.yml

web:
  build: .
  volumes:
    - .:/my_app
  ports:
    - "3000:3000"
  links:
    - postgres
    - redis
    - mailcatcher
  env_file:
    - 'config/application.yml'
postgres:
  image: postgres:9.4
  ports:
    - "5432"
  env_file:
    - 'config/database.yml'
redis:
  image: redis:3.0.6
mailcatcher:
  image: schickling/mailcatcher
  ports:
    - "1080:1080"

回答by Olalekan Sogunle

You might want to update your compose and database yml as follows. With the expected db user and password in the database.yml. Also, you can make this is an environment variable. But try the default for the postgres db docker image first as follows;

您可能希望按如下方式更新您的撰写和数据库 yml。在database.yml. 此外,您可以将其设为环境变量。但是首先尝试 postgres db docker 镜像的默认值,如下所示;

database.yml

database.yml

development:
  database: my_app_development
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres
  password:
  host: postgres(db name in docker-compose.yml)

docker-compose.yml

docker-compose.yml

      web:
        build: .
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
        volumes:
          - .:/my_app
        ports:
          - "3000:3000"
        links:
          - postgres
          - redis
          - mailcatcher
      postgres:
        image: postgres:9.4
        ports:
          - "5432"
      redis:
        image: redis:3.0.6
      mailcatcher:
        image: schickling/mailcatcher
        ports:
            - "1080:1080"

I dont think you want to keep

我不认为你想保留

   env_file:
      - 'config/database.yml'

and

   env_file:
      - 'config/application.yml'

Then create the databases with docker-compose run web rake db:create

然后创建数据库 docker-compose run web rake db:create

I added the command instruction because I dont know what your Dockerfilelooks like. But if you have a successful build of the app image with docker build -t app-name ., you can remove it and just run docker-compose up.

我添加了命令指令,因为我不知道你的Dockerfile样子。但是,如果您使用 成功构建了应用程序映像docker build -t app-name .,则可以将其删除并运行docker-compose up.

回答by Samuel

Without an explicit user set in the database.ymlit will attempt to use the rootuser as this is the same user postgres is running under in the container. To fix this, try setting your database.ymlas:

如果没有在其中设置显式用户database.yml,它将尝试使用该root用户,因为这与 postgres 在容器中运行的用户相同。要解决此问题,请尝试将您设置database.yml为:

development:
  database: my_app_development
  adapter: postgresql
  encoding: unicode
  pool: 5
  username: postgres

Note the addition of the usernamefield.

请注意添加了用户名字段。

回答by Aref Aslani

Postgres image expects POSTGRES_USER, POSTGRES_PASSWORDand POSTGRES_DBto be provided. Otherwise it will use default values. Create a .envfile in the root directory of your project:

Postgres的图像期望POSTGRES_USERPOSTGRES_PASSWORD并且POSTGRES_DB被提供。否则它将使用默认值。.env在你的项目根目录下创建一个文件:

# .env file
POSTGRES_USER=testuser
POSTGRES_PASSWORD=testpass
POSTGRES_DB=db_development

and change you docker-composefile as:

并将您的docker-compose文件更改为:

  web:
    build: .
    volumes:
      - .:/my_app
    ports:
      - 3000:3000
    depends_on:
      - postgres
      - redis
      - mailcatcher
  postgres:
    image: postgres:9.4
    ports:
      - 5432:5432
    env_file: .env
  redis:
    image: redis:3.0.6
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - 1080:1080

You could also provide the environment variables without .envfile:

您还可以提供没有.env文件的环境变量:

  web:
    build: .
    volumes:
      - .:/my_app
    ports:
      - 3000:3000
    depends_on:
      - postgres
      - redis
      - mailcatcher
  postgres:
    image: postgres:9.4
    ports:
      - 5432:5432
    environment:
      - POSTGRES_USER=testuser
      - POSTGRES_PASSWORD=testpass
      - POSTGRES_DB=db_development
  redis:
    image: redis:3.0.6
  mailcatcher:
    image: schickling/mailcatcher
    ports:
      - 1080:1080

and update your database.ymlfile:

并更新您的database.yml文件:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['POSTGRES_USER'] %>
  password: <%= ENV['POSTGRES_PASSWORD'] %>
  host: postgres
development:
  <<: *default
  database: db_development
test:
  <<: *default
  database: db_test
production:
  <<: *default
  database: db_production
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 25 } %>

WARNING: Don't use links, it'll be removed soon

警告:不要使用links它将很快被删除

回答by LeoT

With docker-compose, it tries to preserve volumes between runs; since the user is only created on a new database, you probably need to docker-compose rm -v databaseto delete the container and associated volume.

使用 docker-compose,它会尝试在运行之间保留卷;由于用户仅在新数据库上创建,因此您可能需要docker-compose rm -v database删除容器和关联的卷。

And try docker-compose up --buildagain.

再试docker-compose up --build一次。

Source: https://github.com/docker-library/postgres/issues/41#issuecomment-167603905

来源:https: //github.com/docker-library/postgres/issues/41#issuecomment-167603905