Ruby on rails 如何让PostgreSQL正常工作

Ruby on rails 如何让PostgreSQL正常工作,ruby-on-rails,postgresql,heroku,Ruby On Rails,Postgresql,Heroku,我最近部署了一个heroku应用程序,它给了我一个错误,即sqlite3不受支持,因此我使用rails new-d postgresql project\u name命令创建了一个新项目。在我添加了一个scaffold:rails g scaffold Projects,然后进入routes.rb并添加了,:only[:index,:show],然后运行服务器之前,一切都正常(它目前有一个稳定的版本,人们可以在web上访问)。在我加载主页之前一直有效,然后我得到了错误: PG::Connecti

我最近部署了一个heroku应用程序,它给了我一个错误,即sqlite3不受支持,因此我使用
rails new-d postgresql project\u name
命令创建了一个新项目。在我添加了一个scaffold:
rails g scaffold Projects
,然后进入routes.rb并添加了
,:only[:index,:show]
,然后运行服务器之前,一切都正常(它目前有一个稳定的版本,人们可以在web上访问)。在我加载主页之前一直有效,然后我得到了错误:

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"?
):
这里也有类似的问题,但它们通常在Heroku这边出错,而这只是在本地运行

是什么导致了这个错误

有趣的事实:
/var/run/postgresql/
不是我的目录

编辑: OS:Oracle虚拟机上的Ubuntu

完整控制台响应:

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"?
):

pg (1.0.0) lib/pg.rb:56:in `initialize'
pg (1.0.0) lib/pg.rb:56:in `new'
pg (1.0.0) lib/pg.rb:56:in `connect'
activerecord (5.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:697:in `connect'
activerecord (5.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:221:in `initialize'
activerecord (5.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `new'
activerecord (5.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:38:in `postgresql_connection'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
activerecord (5.1.6) lib/active_record/connection_handling.rb:116:in `retrieve_connection'
activerecord (5.1.6) lib/active_record/connection_handling.rb:88:in `connection'
activerecord (5.1.6) lib/active_record/migration.rb:562:in `connection'
activerecord (5.1.6) lib/active_record/migration.rb:553:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.6.2) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.6.2) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `catch'
web-console (3.6.2) lib/web_console/middleware.rb:20:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.5) lib/rack/method_override.rb:22:in `call'
rack (2.0.5) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
railties (5.1.6) lib/rails/engine.rb:522:in `call'
puma (3.11.4) lib/puma/configuration.rb:225:in `call'
puma (3.11.4) lib/puma/server.rb:632:in `handle_request'
puma (3.11.4) lib/puma/server.rb:446:in `process_client'
puma (3.11.4) lib/puma/server.rb:306:in `block in run'
puma (3.11.4) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
database.yml:

# PostgreSQL. Versions 9.1 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: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: rileyshaw_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: rileyshaw

  # 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: rileyshaw_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: rileyshaw_production
  username: rileyshaw
  password: <%= ENV['RILEYSHAW_DATABASE_PASSWORD'] %>
#PostgreSQL。支持9.1及以上版本。
#
#安装pg驱动程序:
#gem安装页面
#在使用自制软件的OS X上:
#gem安装pg----使用pg config=/usr/local/bin/pg_config
#在带有MacPorts的OS X上:
#gem安装pg--with pg config=/opt/local/lib/postgresql84/bin/pg_-config
#在Windows上:
#gem安装页面
#选择win32版本。
#安装PostgreSQL并将其/bin目录放在您的路径上。
#
#使用Gemfile配置
#宝石“pg”
#
默认值:&默认值
适配器:postgresql
编码:unicode
#有关连接池的详细信息,请参阅Rails配置指南
# http://guides.rubyonrails.org/configuring.html#database-汇集
游泳池:
发展:

看来你没有安装postgres,也没有添加合适的用户。
应该是您的答案:)

将此添加到您的文件中

group: production do 
    gem "pg"
end


group: development do
    gem "sqlite3"
end
然后
bundle安装


gem“Pg”用于生产目的,而gem“sqlite3”仍然可以在本地使用,但我强烈建议您安装Postgres并在系统上运行,以避免将来崩溃。但是如果您正在进行快速原型设计,那么请继续

您使用的是什么操作系统?数据库服务器日志说什么?还有哪些打印到控制台的内容没有包括在这里?你的database.yml是如何配置的?@MarsAtomic我还没有触及.yml,所以这应该是默认值,但我会更新这个问题,以包含你所问的问题。你是否在本地安装了postgres?rails gem仅将其配置为与您的应用程序一起运行。您仍然需要单独安装postgres并添加一个用户。@bkunzi01我不这么认为,我只是遵循heroku的教程,该教程只让我安装了gem。我试试看that@bkunzi01下载了,但现在我收到了
,原因是:PG::ConnectionBad:FATAL:role“riley”不存在
,这似乎是另一个问题。谢谢你的帮助