简体   繁体   English

Rails 6 Docker 与 PostgreSQL 连接错误

[英]Rails 6 Docker with PostgreSQL Connection Error

I'm currently starting with Docker and trying to adapt my first application with a Docker- and a Docker-compose file.我目前从 Docker 开始,并尝试使用 Docker- 和 Docker-compose 文件调整我的第一个应用程序。

It seems to work fine, until I reach the point where I want to create my database.它似乎工作正常,直到我达到我想要创建我的数据库的地步。 I set up the environment variables correctly and reflected them in my database.yml file我正确设置了环境变量并将它们反映在我的 database.yml 文件中

# .env

POSTGRES_USER='deploy'
POSTGRES_PASSWORD='deploy'
POSTGRES_DB='nkbrf_db'

    # database.yml

    default: &default
      adapter: postgresql
      encoding: unicode
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      username: <%= ENV['POSTGRES_USERNAME'] %>
      password: <%= ENV['POSTGRES_PASSWORD'] %>
      database: <%= ENV['POSTGRES_DB'] %>
      host: db

    development:
      <<: *default

    test:
      <<: *default

production:
  <<: *default

Here's my docker-compose.yml:这是我的 docker-compose.yml:

version: '3'
services:
    db:
        image: postgres
        volumes:
        - 'postgres:/var/lib/postgresql/data'
        env_file:
            - '.env'
        ports: 
        - "5432"
    web:
        build: .
        command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes:
            - .:/nkbrfqm
            - bundler_gems:/bundle
        ports:
            - "3000:3000"
        environment:
            RAILS_ENV: development
        depends_on:
            - db
volumes:
    postgres:
    bundler_gems:

It sets up everything correctly but if I try to create my database with它正确设置了所有内容,但是如果我尝试使用

docker-compose run web rake db:create docker-compose 运行 web rake db:create

I get the following error:我收到以下错误:

PG::ConnectionBad: FATAL: password authentication failed for user "root" PG::ConnectionBad: FATAL: 用户“root”的密码认证失败

Where does this user "root" come from?这个用户“root”来自哪里? Is something wrong with my environment variable settings?我的环境变量设置有问题吗?

You need to remove single-quote ' in the .env file then you need to remove all containers in the compose file( docker-compose rm ).您需要删除.env文件中的单引号'然后您需要删除撰写文件中的所有容器( docker-compose rm )。 then execute docker-compose up -d然后执行docker-compose up -d

Example例子

#.env

POSTGRES_USER=deploy POSTGRES_PASSWORD=deploy POSTGRES_DB=nkbrf_db

Thanks谢谢

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

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