【问题标题】:Changing sqlite to PostgreSQL in ruby on rails在 ruby​​ on rails 中将 sqlite 更改为 PostgreSQL
【发布时间】:2018-10-22 23:37:36
【问题描述】:

我在 docker 中使用 win 7,在 docker 中我在 rails 上使用 ruby​​,我做了一个与 sqlite3 配合得很好的应用程序,但是当我尝试上传到 Heroku 时,Heroku 给了我一个错误,它不支持 sqlite3所以我不得不将我的应用程序数据库更改为 PostgreSQL,并进行了以下更改: 在我的 Gemfile 中,我评论了 gem sqlite3:

# Use sqlite3 as the database for Active Record
#gem 'sqlite3'
#posgresql
gem 'pg'

第二次我做了一个“捆绑安装”,然后是一个 docker-compuse 构建

然后在我的 database.yml 中:

之前:

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

改动后:

development:
  adapter: postgresql
  database: my_database_development
  pool: 5
  timeout: 5000

test:
  adapter: postgresql
  database: my_database_test
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: my_database_production
  pool: 5
  timeout: 5000

所以当我尝试使用“rake db:create”创建新的数据库时,docker 给了我下一个错误:

 $ docker-compose run web rails db:create
Starting grupo43_postgres_1 ... done
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"?
Couldn't create database for {"adapter"=>"postgresql", "database"=>"my_database_
development", "pool"=>5, "timeout"=>5000}
rails aborted!
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"?
/usr/local/bundle/gems/pg-1.0.0/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-1.0.0/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-1.0.0/lib/pg.rb:56:in `connect'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
postgresql_adapter.rb:697:in `connect'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
postgresql_adapter.rb:221:in `initialize'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
postgresql_adapter.rb:38:in `new'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
postgresql_adapter.rb:38:in `postgresql_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:759:in `new_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:803:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:743:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:500:in `checkout'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:374:in `connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_adapters/
abstract/connection_pool.rb:931:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_handling.
rb:116:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/connection_handling.
rb:88:in `connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/postgresql_dat
abase_tasks.rb:10:in `connection'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/postgresql_dat
abase_tasks.rb:19:in `create'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks
.rb:117:in `create'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks
.rb:137:in `block in create_current'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks
.rb:305:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks
.rb:302:in `each'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks
.rb:302:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/tasks/database_tasks
.rb:136:in `create_current'
/usr/local/bundle/gems/activerecord-5.1.6/lib/active_record/railties/databases.r
ake:27:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/railties-5.1.6/lib/rails/commands/rake/rake_command.rb:21
:in `block in perform'
/usr/local/bundle/gems/railties-5.1.6/lib/rails/commands/rake/rake_command.rb:18
:in `perform'
/usr/local/bundle/gems/railties-5.1.6/lib/rails/command.rb:46:in `invoke'
/usr/local/bundle/gems/railties-5.1.6/lib/rails/commands.rb:16:in `<top (require
d)>'
/example/bin/rails:9:in `require'
/example/bin/rails:9:in `<top (required)>'
/usr/local/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
/usr/local/bundle/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
/usr/local/bundle/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
/usr/local/bundle/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
/usr/local/bundle/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
/usr/local/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
/usr/local/bundle/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)
>'
/example/bin/spring:15:in `require'
/example/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)

我找到了不同的解决方案,但对于 linux 和 mac,我可以在这里告知的是,问题可能是因为我必须删除一些我不知道在哪里可以使用 windows 或 docker 找到的文件 :(,所有我找到的信息在这里:PG::ConnectionBad - could not connect to server: Connection refused

这是我的 docker-compose.yml 文件:

version: '3'
services:
  postgres:
    image: postgres:10.3
    ports:
      - "3000"
  web:
    build: .
    env_file:
      - .env
    command: bash -c "(bundle check || bundle install) && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/example
    ports:
      - "3000:3000"
    depends_on:
      - postgres

【问题讨论】:

  • 你在你的开发机器上安装了 postgresql 吗? sqlite3 与 rails 捆绑在一起,但需要安装其他 SQL 数据库。查看postgresql.org/download 但是...您可以继续在 sqlite3 上进行开发和测试,并且仅将 postgresql 用于生产(即 heroku)
  • 另外,您需要为 postgresql 创建具有正确权限的用户。然后将用户名、密码、端口和主机添加到 database.yml
  • 检查数据库是否正在运行或再次运行。在本地使用终端总是更容易,但是在 docker 中你有类似的方法:command line in docker
  • 请edit您的问题并添加您的docker-composer.yml文件。

标签: ruby-on-rails ruby postgresql docker heroku


【解决方案1】:

对我来说最简单的方法是

  • 创建一个像这样的新应用rails new testapp --database=postgresql
  • 打开 config/database.yml
  • 将该文件中的所有内容复制到我现有的应用中
  • 在该文件的大约 4 个位置更改名称
  • 一切正常!

此解决方案的一个优点是您可以将其用于任何 Rails 版本(只需确保您创建的新应用与您要切换其数据库的版本相同)。

【讨论】:

    【解决方案2】:

    确保您已在您的机器中安装了PostgreSQL,并添加了一个名为pgAdmin 的有用工具,然后更新database.yml 文件,如下所示

    default: &default
      adapter: postgresql
      encoding: unicode
      username: postgres
      password: xxxxx #<-- which you have defiend while installing postgresql
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
    
    development:
      <<: *default
      database: project_development
    
    test:
      <<: *default
      database: project_test
    
    production:
      <<: *default
      database: project_production
      username: username
      password: <%= ENV['PROJECT_DATABASE_PASSWORD'] %>
    

    和 gem pg 在你的 Gemfile 中使用这个版本,比如 gem 'pg', '~&gt; 0.20.0' 然后运行 ​​bundle install

    rake db:create
    rake db:migrate
    

    对于 Heroku

    heroku run rake db:migrate
    

    如果你的 Rails 版本是rails &gt; 5.0.0,你可以使用rails 命令而不是rake

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 2011-10-06
      • 1970-01-01
      相关资源
      最近更新 更多