【问题标题】:Rails 4: changing database to postgresql (from sqlite3)Rails 4:将数据库更改为 postgresql(来自 sqlite3)
【发布时间】:2016-06-11 16:10:52
【问题描述】:

所以我想将我的数据库表单 sqlite3 更改为 pg。我尝试遵循this 指南。有一些关于数据库密码的东西,我不知道我应该把那个密码放在哪里。有没有默认的?

宝石文件

source 'https://rubygems.org'

gem 'rails', '4.2.5.1'
gem 'pg'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'decent_exposure'
gem 'slim'
gem 'nprogress-rails'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'ffaker'

group :development, :test do
  gem 'pry'
end

group :development do
  gem 'pry-rails'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'spring'
  # gem 'sqlite3'
end

数据库.yml

default: &default
  adapter: postgresql
  host: localhost
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: blog_development

test:
  <<: *default
  database: blog_test

production:
  <<: *default
  database: blog_production

捆绑安装结果是 100% 成功。

当我尝试创建数据库时:

bin/rake db:create
Running via Spring preloader in process 12795
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"?
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/home/dominik/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/home/dominik/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:87:in `connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/postgresql_database_tasks.rb:6:in `connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/postgresql_database_tasks.rb:15:in `create'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:93:in `create'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:274:in `each'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:106:in `create_current'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:17:in `block (2 levels) in <top (required)>'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:240:in `call'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:240:in `block in execute'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:235:in `each'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:235:in `execute'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/dominik/.rvm/rubies/ruby-2.2.4/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/home/dominik/github/dd_blog/bin/rake:9:in `<top (required)>'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `block in load'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/home/dominik/.rvm/rubies/ruby-2.2.4/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/home/dominik/.rvm/rubies/ruby-2.2.4/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"blog_development"}

rake db:迁移

rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/connection_handling.rb:87:in `connection'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:916:in `initialize'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:823:in `new'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:823:in `up'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:801:in `migrate'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/home/dominik/.rvm/gems/ruby-2.2.4/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/home/dominik/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `eval'
/home/dominik/.rvm/gems/ruby-2.2.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

请注意,我之前从未使用过 postgres。也许需要一些配置?

附:我正在使用 Ubuntu 14.04

原来,我没有安装 postgresql。

【问题讨论】:

    标签: ruby-on-rails postgresql ruby-on-rails-4 sqlite


    【解决方案1】:

    有几件事,rake db:migrate 只会运行您的迁移,不会创建您的开发 deb。创建数据库的命令是: 耙分贝:创建

    您只能在创建数据库并连接到数据库后运行迁移。

    所以先尝试一下,如果失败,请检查以确保您正在运行 postgresql。我在 ubuntu 15.04 上,我将 postgres 作为服务运行,如果需要,我会考虑这样做。确认 postgresql 已正确安装并运行。在终端 'psql -V' 将返回您安装的 postgresql 版本。检查进程 $ ps aux | grep postgresl 。

    一旦您确认您已经启动并运行了 postgresql。接下来,从 database.yml 中删除 'host: localhost' 行,这不是必需的。接下来,在 database.yml 中尝试添加: port: 5432 在您的开发数据库下方。如果这仍然不能解决它下面的添加: host: ""

    【讨论】:

      【解决方案2】:

      1 - 将此添加到您的 Gemfile 中

      gem 'pg', '~> 0.15'
      

      2 - 将此添加到您的 database.yml 中,将 myapp 更改为您的应用程序名称

      # PostgreSQL. Versions 8.2 and up are supported.
      #
      # Install the pg driver:
      #   gem install pg
      # On OS X with Homebrew:
      #   gem install pg -- --with-pg-config=/usr/local/bin/pg_config
      # On OS X with MacPorts:
      #   gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
      # On Windows:
      #   gem install pg
      #       Choose the win32 build.
      #       Install PostgreSQL and put its /bin directory on your path.
      #
      # Configure Using Gemfile
      # gem 'pg'
      #
      default: &default
        adapter: postgresql
        encoding: unicode
        # For details on connection pooling, see rails configuration guide
        # http://guides.rubyonrails.org/configuring.html#database-pooling
        pool: 5
      
      development:
        <<: *default
        database: myapp_development
      
        # The specified database role being used to connect to postgres.
        # To create additional roles in postgres see `$ createuser --help`.
        # When left blank, postgres will use the default role. This is
        # the same name as the operating system user that initialized the database.
        #username: myapp
      
        # The password associated with the postgres role (username).
        #password:
      
        # Connect on a TCP socket. Omitted by default since the client uses a
        # domain socket that doesn't need configuration. Windows does not have
        # domain sockets, so uncomment these lines.
        #host: localhost
      
        # The TCP port the server listens on. Defaults to 5432.
        # If your server runs on a different port number, change accordingly.
        #port: 5432
      
        # Schema search path. The server defaults to $user,public
        #schema_search_path: myapp,sharedapp,public
      
        # Minimum log levels, in increasing order:
        #   debug5, debug4, debug3, debug2, debug1,
        #   log, notice, warning, error, fatal, and panic
        # Defaults to warning.
        #min_messages: notice
      
      # 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: myapp_test
      
      # As with config/secrets.yml, you never want to store sensitive information,
      # like your database password, in your source code. If your source code is
      # ever seen by anyone, they now have access to your database.
      #
      # Instead, provide the password as a unix environment variable when you boot
      # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
      # for a full rundown on how to provide these environment variables in a
      # production deployment.
      #
      # On Heroku and other platform providers, you may have a full connection URL
      # available as an environment variable. For example:
      #
      #   DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
      #
      # You can use this database configuration with:
      #
      #   production:
      #     url: <%= ENV['DATABASE_URL'] %>
      #
      production:
        <<: *default
        database: myapp_production
        username: myapp
        password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
      

      2 - 然后:

      rake db:purge
      rake db:setup
      rake db:migrate
      

      【讨论】:

        【解决方案3】:

        导轨 6+

        在 Rails 6.0 或更高版本中,您可以运行以下命令:

        rails db:system:change --to=postgresql
        

        它还接受mysqlsqlite3oraclesqlserver 和其他可以在this class 中看到的内容。

        【讨论】:

          猜你喜欢
          • 2018-07-04
          • 1970-01-01
          • 2021-05-21
          • 2021-06-29
          • 1970-01-01
          • 2021-08-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多