【问题标题】:Ruby on Rails PG::ConnectionBadRuby on Rails PG::ConnectionBad
【发布时间】:2017-12-19 18:38:00
【问题描述】:

当我运行命令时,我正在尝试部署我的应用程序

rails server -b $IP -p $PORT

我得到一个错误

PG::ConnectionBad

无法连接到服务器:连接被拒绝服务器是否在本地运行并接受 Unix 域套接字“/var/run/postgresql/.s.PGSQL.5432”上的连接?

提取的源代码(第 56 行附近):

### Convenience alias for PG::Connection.new.
def self::connect( *args )
    return PG::Connection.new( *args )
end

我的 database.yml 是

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
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: 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

======= 更新 =====

我现在收到一条错误消息,提示 ActiveRecord::NoDatabaseError, 致命:数据库“sale_development”不存在

当我将 database.yml 更新为

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
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:
  adapter: postgresql
  encoding: unicode
  database: sale_development
  pool: 5

test: &test
  adapter: postgresql
  encoding: unicode
  database: sale_test
  pool: 5

production:
  adapter: postgresql
  encoding: unicode
  database: sale_production
  pool: 5
  host: localhost
  username: sale
  password: <%= ENV['SALE_DATABASE_PASSWORD'] %>

=> 启动彪马 退出' 网络控制台 (3.5.1) lib/web_console/middleware.rb:135:in call_app'actionpack (5.1.1) lib/action_dispatch/middleware/request_id.rb:25:incall' 机架 (2.0.3) lib/rack/method_override.rb:22:in call' rack (2.0.3) lib/rack/runtime.rb:22:incall' activesupport (5.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in call' puma (3.8.2) lib/puma/thread_pool.rb:120:inblock in spawn_thread' ActiveRecord::StatementInvalid: PG::InvalidParameterValue: ERROR: new encoding (UTF8) is incompat17:in create' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:ineach' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:3Tasks: TOP => db:create ActiveRecord::StatementInvalid: PG::InvalidParameterValue: ERROR: new encoding (UTF8) is incompat17:in create' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:ineach' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:in each_current_configuration' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:136:increate_current' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:2/usr/local/rvm/gems/ruby-2.3.0/bin /ruby_executable_hooks:15:in eval' /usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/migration.rb:1007:in up' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:ineach' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:3/usr/local/rvm/gems/ruby-2.3.0/gems /activesupport-5.1.1/lib/active_support/dependencies/interlock./usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:在each'/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:ineach_current_configuration'/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:136:in create_current'/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:27:inblock(2级)在'/usr/local/rvm/gems/ruby-2.3.0/gems/rake-12.0.0/exe/rake:27:in <top (required)>' /usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:ineval' /usr/local/rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `' 任务:TOP => db:create (通过使用 --trace 运行任务查看完整跟踪)

===== 更新 ====

宝石文件

 source 'https://rubygems.org'

 git_source(:github) do |repo_name|
   repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
   "https://github.com/#{repo_name}.git"
 end


 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '~> 5.1.1'
 # Use sqlite3 as the database for Active Record
 gem 'sqlite3'
 gem 'pg'
 # Use Puma as the app server
 gem 'puma', '~> 3.7'
 # Use SCSS for stylesheets
 gem 'sass-rails', '~> 5.0'
 # Use Uglifier as compressor for JavaScript assets
 gem 'uglifier', '>= 1.3.0'
 # See https://github.com/rails/execjs#readme for more supported runtimes
 # gem 'therubyracer', platforms: :ruby

 # Use CoffeeScript for .coffee assets and views
 gem 'coffee-rails', '~> 4.2'
 # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
 gem 'turbolinks', '~> 5'
 # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'bootstrap-sass'
gem "paperclip", "~> 5.0.0"
gem 'devise', '~> 4.3'
gem 'will_paginate', '~> 3.0'


# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
   # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

【问题讨论】:

  • 只要运行 rake db:create 来创建你的数据库,然后运行 ​​rake db:migrate 如果你已经有一个模型
  • 如果您的服务器是新的并且仍然没有数据并且它是生产服务器,则运行 RAILS_ENV=production rake db:create 然后继续 RAILS_ENV=production rake db:migrate
  • 我试过了,但得到一长串错误
  • 你能在上面粘贴10行吗?
  • 检查你的 Gemfile,你有行 gem 'pg' 吗?

标签: ruby-on-rails postgresql heroku


【解决方案1】:

我认为它显示了与 sqlite3 的连接,如果是生产那么你必须输入用户名和密码

如果使用 postgresql,这是供您参考

development:
  adapter: postgresql
  encoding: unicode
  database: your_database_name
  pool: 5

test: &test
  adapter: postgresql
  encoding: unicode
  database: your_database_name_test
  pool: 5

production:
  adapter: postgresql
  encoding: unicode
  database: your_database_name_production
  pool: 5
  host: localhost
  username: some_user_name
  password: some_password

【讨论】:

  • 我的文件名是sale,那么sale是数据库的名称吗?对不起,如果我听起来很愚蠢,但我对此很陌生
  • 我们都在这里学习,如果您确切地知道您的数据库名称为 sale,那么您可以在生产部分数据库中更改:sale,如果数据库服务器位于不同的服务器中,请确保您将主机从 localhost 更改为数据库IP
  • 好像不管怎么做都无法摆脱错误
  • 标记 gem 'sqlite3',因为你打算使用 pg,所以让它不活跃
  • 我这样做了,没什么区别,一直说“sale_development”不存在,但它应该是因为“sale”是我使用 rails new sale 创建文件夹时的名称
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
相关资源
最近更新 更多