16

我正在尝试运行rake db:create,或者rake db:setup出现以下错误。这很奇怪,因为rake db:create它在其他 Rails 项目的其他文件夹中运行良好。

user:project user$ rake db:create
could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `initialize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `new'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `connect'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:260:in `initialize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:27:in `new'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:27:in `postgresql_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:304:in     `new_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:323:in `checkout_new_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block (2     levels) in checkout'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `loop'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `block in checkout'
/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:260:in `checkout'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:162:in `connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in     `retrieve_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:110:in `rescue in create_database'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:54:in `create_database'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-    3.1.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/user/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/Users/user/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "database"=>"project_test",     "username"=>nil, "password"=>nil, "min_messages"=>"warning"}

这是我的database.yml

development:
  adapter: postgresql
  database: project_development
  username:
  password:
  min_messages: warning

test:
  adapter: postgresql
  database: project_test
  username:
  password:
  min_messages: warning

production:
  adapter: postgresql
  database: project_production
  username:
  password:
  min_messages: warning

Posgree 在/usr/local/var/postgres. rake 不应该尝试在同一个文件夹中查找它而不是/var/pgsql_socket/? (反正我没有 /usr/local/var/pgsql_socket )

4

9 回答 9

20

解决了!

解决方案很简单,可以更新当前安装的 gem。

另一种尝试的解决方案是设置host: localhost或您在 database.yml 文件的开发设置中使用的任何主机。

于 2012-05-26T06:09:21.510 回答
8

我通过 Homebrew 在 Mac OS X 上安装了 PG(我没有意识到它在 Mac OS X 中是默认的),并且遇到了同样的问题。在通过 Homebrew 安装 pg 之前安装 pg gem 可能会使问题变得更糟。

我通过执行以下操作解决了同样的问题:

  1. 确保所有二进制文件都指向 Cellar 目录中的二进制文件(Mac OS X 上的 /usr/local/Cellar)
  2. 卸载 pg gemgem uninstall pg
  3. 使用 Homebrew 配置重新安装gem install pg -- --with-pg-config=/usr/local/bin/pg_config(如果您使用 pg setup 创建了一个 rails 项目,这在您的 database.yml 配置文件中进行了说明)

它奏效了!

于 2013-10-26T16:46:41.030 回答
5

首先,您需要在 Postgres 中手动创建用户,同时为该用户授予创建数据库的权限(AKA createdb)。

为此,首先,打开 Postgres shell 提示符

$ sudo -u postgres psql

创建您的用户

postgres=# create role Your_DB_username with createdb login password 'YOUR_PASSWORD';

退出 Postgres 外壳

postgres=# \q

执行任何与 db 相关的 rake 任务:

$ rake db:create

它会起作用的!

于 2015-11-19T17:47:28.960 回答
4

上面 zquintana 的答案对我有用,但有一些小警告。

我的解决方案(使用 Homebrew):

  1. 如果你安装了 gemgem uninstall pg
  2. 删除 postgresql 的旧副本brew uninstall postgresql
  3. 重新安装postgresqlbrew install postgresql
  4. 安装说明提到:

要在登录时启动 postgresql:

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

然后现在加载 postgresql:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

或者,如果您不想/不需要launchctl,您可以运行:

postgres -D /usr/local/var/postgres

  1. 所以运行ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

  2. 然后launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

  3. 现在使用 Homebrew 配置重新安装 gemgem install pg -- --with-pg-config=/usr/local/bin/pg_config

  4. 现在运行你的rake db:create

于 2016-02-18T21:51:09.987 回答
4

就我而言,这就是我所需要的:https ://dba.stackexchange.com/a/75221

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
于 2016-05-24T16:16:44.427 回答
2

我遇到了相同的问题,并且能够通过以下方式解决错误:

  1. 使用 postgresapp.com(由 heroku 在https://devcenter.heroku.com/articles/heroku-postgresql#local-setup推荐),
  2. 添加到 config/database.yml

    host: localhost
    
  3. 按照http://postgresapp.com/documentation故障排除部分的建议重置共享内存设置:

    sudo sysctl -w kern.sysv.shmall=65536
    sudo sysctl -w kern.sysv.shmmax=16777216
    
于 2013-10-28T18:58:47.427 回答
1

还要检查您没有打开的连接,例如连接到实例的客户端或应用程序。如果发生这种情况,您将在错误中看到这一点

Couldn't create database for {"adapter"=>"xxxx", "database"=>"xxxx", "host"=>"localhost", "username"=>"postgres", "password"=>"xxxxx", "pool"=>5, "timeout"=>5000}
rake aborted!
ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR:  source database "template1" is being accessed by other users
DETAIL:  There is 1 other session using the database.
于 2014-12-11T01:08:23.040 回答
1

简单添加host: localhost到您的 database.yml

更多详情请访问rake db:create:all failed to connect to PostgreSQL 数据库

于 2019-05-16T06:47:52.643 回答
0

我可以通过确保 database.yml 文件中的数据库名称采用以下格式来解决此问题:appname_development、appname_test 和 appname_production。这是我的 database.yml 文件作为示例:

    development:
      adapter: postgresql
      database: parallax_development
      username:
      password:

    test:
      adapter: postgresql
      database: parallax_test
      username:
      password:

    production:
      adapter: postgresql
      database: parallax_production
      username:
      password:
于 2014-04-23T17:19:44.447 回答