如何使用 rails 创建数据库?问题

How I can create database with rails? Problem

首先,我使用 windows 11 和 WSL2 ubuntu 20.04。 我正在使用 docker 构建映像,其中安装 ruby 和 postgresql。

docker-compose.yml

version: '3.9'

services:
  db:
    image: 'postgres:14.1-bullseye'
    volumes:
      - 'postgres:/var/lib/postgresql/data'
    ports:
      - '5432:5432'
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust

  redis:
    image: 'redis:6.2.6-bullseye'
    command: redis-server
    ports:
      - '6379:6379'
    volumes:
      - 'redis:/data'

  web:
    depends_on:
      - 'db'
      - 'redis'
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    ports:
      - '3002:3000'
    environment:
      - DATABASE_HOST=db

volumes:
  redis:
  postgres:

ruby 3.1.2-bullseye

一切正常,我用 rails new project-name --api --database=postgresql -T 创建了一个新项目,docker compose build 工作正常。 之后我想用 docker compose run web rails db:create 创建一个数据库,现在问题出现了。

我收到这个错误:

We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create
Couldn't create 'greenhouse_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: We could not find your database: postgres. Which can be found in the database configuration file located at config/database.yml.

To resolve this issue:

- Did you create the database for this app, or delete it? You may need to create your database.
- Has the database name changed? Check your database.yml config has the correct database name.

To create your database, run:

        bin/rails db:create


Caused by:
PG::ConnectionBad: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Tasks: TOP => db:create
(See full trace by running task with --trace)

我不知道发生了什么事,求助并在我做错的地方签字谢谢提前谢谢。

更新 database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see Rails configuration guide
  # https://guides.rubyonrails.org/configuring.html#database-pooling
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV.fetch("DATABASE_HOST") { "db" } %>
  port: <%= ENV.fetch("DATABASE_PORT") { 5432 } %>
  username: <%= ENV.fetch("DATABASE_USERNAME") { "postgres" } %>
  password: <%= ENV.fetch("DATABASE_PASSWORD") { "password" } %

development:
  <<: *default
  database: greenhouse_development

test:
  <<: *default
  database: greenhouse_test

production:
  <<: *default
  database: greenhouse_production
  username: greenhouse
  password: <%= ENV["GREENHOUSE_DATABASE_PASSWORD"] %>

我依稀记得在数据库配置文件中加载环境变量时有些奇怪。如果您使用 nulldb adapter:

,则可以在 docker 正在构建时在没有数据库连接的情况下初始化 rails
# database.build.yml 
build:
  adapter: nulldb
FROM somebase as build
...
RUN cp ./config/database.build.yml ./config/database.yml 
  && RAILS_ENV=build bundle exec rake assets:precompile

很遗憾,我没有直接解决这个问题。为了从总体上解决这个问题,我下载了一个示例 rails 7 docker 存储库,并且只更改了我自己的 API。 这是我使用 https://github.com/ryanwi/rails7-on-docker

的存储库的 link