Ruby on Rails PG::ConnectionBad

3

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

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, FATAL: 数据库 "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'] %>

=> 正在启动 Puma 服务器 web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app' actionpack (5.1.1) lib/action_dispatch/middleware/request_id.rb:25:in `call' rack (2.0.3) lib/rack/method_override.rb:22:in `call' rack (2.0.3) lib/rack/runtime.rb:22:in `call' 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:in `block in spawn_thread' ActiveRecord::StatementInvalid: PG::InvalidParameterValue: 错误:新编码(UTF8)不兼容 /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:in `each' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:in `create_current' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:27:in `block (2 levels) in ' Tasks: TOP => db:create PG::InvalidParameterValue: 错误:新编码(UTF8)不兼容 /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:in `each' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/tasks/database_tasks.rb:303:in `create_current' /usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.1.1/lib/active_record/railties/databases.rake:27:in `block (2 levels) in ' (使用 --trace 参数可以查看完整的追踪信息)

===== UPDATE ====

Gemfile

 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。 - widjajayd
如果您的服务器是新的且还没有数据,并且它是生产服务器,则运行RAILS_ENV=production rake db:create,然后继续使用RAILS_ENV=production rake db:migrate。 - widjajayd
你能在上面粘贴10行吗? - widjajayd
1
请检查您的Gemfile文件,是否有一行gem 'pg'? - widjajayd
好的,既然它在生产服务器上,你也在生产服务器上安装了pg gem吗? - widjajayd
显示剩余5条评论
1个回答

0

我的观点是显示与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”就是数据库的名称吗?抱歉如果我听起来很蠢,但我对此非常陌生。 - bockdavidson
我们都在这里学习,如果你确切地知道你的数据库名称为 sale,那么你可以在生产环境部分更改数据库:sale,如果数据库服务器位于不同的服务器上,请确保将主机从 localhost 更改为数据库的 IP。 - widjajayd
无论我做什么,似乎都无法摆脱这个错误。 - bockdavidson
标记 gem 'sqlite3',由于您计划使用 pg,请将其设置为非活动状态。 - widjajayd
我已经尝试过了,但没有任何改变,它仍然显示“sale_development”不存在,但是应该存在,因为我使用rails new sale命令创建了一个名为“sale”的文件夹。 - bockdavidson
这篇文章对于你的情况很有帮助,建议阅读:https://dev59.com/D3I-5IYBdhLWcg3wQV4Z - widjajayd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接