我试图运行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
为单位。我不应该尝试在同一个文件夹中查找它而不是/var/pgsql_socket/
吗? (无论如何我没有/ usr / local / var / pgsql_socket)
答案 0 :(得分:19)
解决!
解决方案很简单,可以更新当前安装的宝石。
另一个尝试的解决方案是设置host: localhost
或您在database.yml文件上的开发设置上使用的任何主机。
答案 1 :(得分:8)
我通过Homebrew在Mac OS X上安装了PG(我没有意识到它在Mac OS X中是默认的),并且遇到了同样的问题。我可能通过在通过Homebrew安装pg之前安装pg gem来解决问题。
我通过执行以下操作解决了同样的问题:
gem uninstall pg
gem install pg -- --with-pg-config=/usr/local/bin/pg_config
重新安装(如果您使用pg setup创建了一个rails项目,这将在您的database.yml配置文件中进行说明)它有效!
答案 2 :(得分:2)
我遇到了同样的问题,并且能够通过以下方式解决错误:
添加到config / database.yml
host: localhost
按照http://postgresapp.com/documentation的问题排查部分中的建议重置共享内存设置:
sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216
答案 3 :(得分:2)
上面zquintana的答案对我有一些小的警告。
我的解决方案(使用Homebrew):
gem uninstall pg
brew uninstall postgresql
brew install postgresql
要在登录时启动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
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
现在使用Homebrew配置重新安装gem gem install pg -- --with-pg-config=/usr/local/bin/pg_config
现在运行您的rake db:create
答案 4 :(得分:2)
就我而言,这就是我所需要的:https://dba.stackexchange.com/a/75221
var myObj = new { MyRootAttr = new { value = "My Value" } };
JavaScriptSerializer js = new JavaScriptSerializer();
string json = js.Serialize(myObj); //{"MyRootAttr":{"value":"My Value"}}
答案 5 :(得分: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.
答案 6 :(得分:1)
首先,您需要在Postgres中手动创建用户,同时为此用户授予数据库创建权限(AKA createdb)。
首先,打开Postgres shell提示符
$ sudo -u postgres psql
创建您的用户
postgres=# create role Your_DB_username with createdb login password 'YOUR_PASSWORD';
退出Postgres shell
postgres=# \q
执行与db相关的任何rake任务:
$ rake db:create
它会起作用!
答案 7 :(得分: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:
答案 8 :(得分:0)
简单地将extend keyof T
添加到您的数据库中。yml
有关更多详细信息,请访问rake db:create:all fails to connect to PostgreSQL database