PG::ConnectionBad: 无法连接到服务器:连接被拒绝

4

当我尝试运行时,出现以下错误

git heroku push master

我不知道如何解决这个问题。我正在使用 Rails 3.2.19

 fernando@fernando:~/ProyectoTicketMaster/Ticket_Master$ git push  heroku master 

Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       Connecting to database specified by DATABASE_URL
       rake aborted!
       PG::ConnectionBad: could not connect to server: Connection refused
       Is the server running on host "127.0.0.1" and accepting
       TCP/IP connections on port 5432?
       /tmp/build_dd87fdc88ebb265d09b8212e3b0df10b/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `initialize'
       /tmp/build_dd87fdc88ebb265d09b8212e3b0df10b/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `new'
       /tmp/build_dd87fdc88ebb265d09b8212e3b0df10b/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.19/lib/active_record/connection_adapters/postgresql_adapter.rb:1222:in `connect'

这是我的GemFile文件。我在生产环境使用pg数据库,在开发环境使用sqlite数据库。以下附上我的GemFile和database.yml文件。

source 'https://rubygems.org'

gem 'rails', '3.2.19'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
  gem 'sqlite3',     '1.3.9'

end

group :production do
  gem 'pg',             '0.17.1'
  gem 'rails_12factor', '0.0.2'
end


gem 'hirb'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'


development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

# 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:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000
2个回答

3

可能有几个原因引起了这个问题。

您是否运行了postgres?是否将其添加到您的服务器中?

您是否已安装了用于pg的gem?

您的设置应如下所示:

development:
  adapter: postgresql
  host: localhost
  username: user
  database: app-dev

您可以查看此链接:https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-ruby,它可能会有所帮助 :)

注意:这应该是一条评论,但我没有足够的声望来评论。


我安装了 gem 'pg',但是我的机器上没有安装 postgres。我该如何安装它?我如何验证我在 ubuntu 12.04 上安装的 postgres 版本?这可能是问题吗? - daniel barrera
我认为这个链接可能会有所帮助:http://robdodson.me/blog/2012/04/27/how-to-setup-postgresql-for-rails-and-heroku/。如果您发现有用,请点赞 :) - Miknash
看起来你已经为sqlite3定义了适配器,如果你正在使用postgres也需要更改。我已经向你发送了pg的格式。 - Miknash
我在生产环境中使用Postgres与Heroku一起工作group :production do gem 'pg', '0.17.1' gem 'rails_12factor', '0.0.2' end - daniel barrera
我在谈论你代码片段的结尾。你有以下内容:development: adapter:sqlite3, production: adapter:slqite3等。 - Miknash

-1
在config/application.rb中添加以下行是解决我的问题的方法。
 config.assets.initialize_on_precompile = false

3
它在哪里?它是什么?不解释任何东西。 - Masoud Dadashi
这应该添加在 config/application.rb 文件中。 - Kannan S

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