ruby-on-rails 如何使用Rails创建数据库?问题

vc9ivgsu  于 2022-11-19  发布在  Ruby
关注(0)|答案(3)|浏览(274)

首先,我使用的是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"] %>
woobm2wo

woobm2wo1#

我依稀记得在db配置文件中加载env变量时有些不稳定。如果使用nulldb adapter
第一个

bvn4nwqk

bvn4nwqk2#

我刚刚遇到了同样的问题。我有同样的系统windows 11 wsl Ubuntu。结果,这只是因为我没有启动postgresql服务器。
所以在航站楼我确实去了这个地方:

cd /var/lib/postgresql

然后道:

sudo service postgresql start

然后返回到您的文件夹(使用您的rails应用程序)并再次执行rake db:create
我希望它能帮助其他遇到同样问题的人。

mcvgt66p

mcvgt66p3#

不幸的是,我并没有直接解决这个问题。为了全面解决这个问题,我下载了一个示例rails7docker存储库,只修改了我自己的API。

相关问题