在Rails中,无法为{“adapter”=>“postgresql”创建数据库,

时间:2022-09-08 13:44:10

I'm trying to run rake db:create or rake db:setup but I get the following error. It's strange because rake db:create it's working fine from others folders with other Rails projects.

我正在尝试运行rake db:create或rake db:setup但是我收到以下错误。这很奇怪,因为rake db:使用其他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"}

This is my database.yml

这是我的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 is insatelled on /usr/local/var/postgres. Shouldn't rake try to look it in the same folder instead of /var/pgsql_socket/ ? (I don't have a /usr/local/var/pgsql_socket anyways)

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

8 个解决方案

#1


18  

Solved!

解决了!

Solution was simple to update current installed gems.

解决方案很容易更新当前安装的宝石。

Another solution to try is set host: localhost or whatever host you use on development settings on database.yml file.

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

#2


8  

I installed PG on Mac OS X via Homebrew (I didn't realize it came default in Mac OS X), and had the same problem. I may have made the problem worse by installing the pg gem before installing pg via Homebrew.

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

I solved this same problem by doing the following:

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

  1. Make sure that all binaries are pointing to binaries in Cellar directory (/usr/local/Cellar on Mac OS X)
  2. 确保所有二进制文件都指向Cellar目录中的二进制文件(Mac OS X上的/ usr / local / Cellar)
  3. Uninstall pg gem gem uninstall pg
  4. 卸载pg gem gem uninstall pg
  5. Reinstall with Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config (if you created a rails project with pg setup, this is explained in your database.yml config file)
  6. 使用Homebrew configs重新安装gem install pg - --with-pg-config = / usr / local / bin / pg_config(如果你使用pg setup创建了一个rails项目,这在你的database.yml配置文件中有解释)

And it worked!

它奏效了!

#3


2  

I encountered an identical issue and was able to solve the error by:

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

  1. using postgresapp.com (recommended by heroku in https://devcenter.heroku.com/articles/heroku-postgresql#local-setup),
  2. 使用postgresapp.com(由heroku在https://devcenter.heroku.com/articles/heroku-postgresql#local-setup中推荐),
  3. adding to config/database.yml

    添加到config / database.yml

    host: localhost
    
  4. resetting shared memory settings as recommended in troubleshooting section of http://postgresapp.com/documentation:

    按照http://postgresapp.com/documentation的问题排查部分中的建议重置共享内存设置:

    sudo sysctl -w kern.sysv.shmall=65536
    sudo sysctl -w kern.sysv.shmmax=16777216
    

#4


2  

The answer from zquintana above worked for me with a few small caveats.

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

My solution (using Homebrew):

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

  1. If you have the gem installed gem uninstall pg
  2. 如果你有宝石安装宝石卸载pg
  3. Remove old copy of postgresql brew uninstall postgresql
  4. 删除postgresql brew卸载postgresql的旧副本
  5. Re-install postgresql brew install postgresql
  6. 重新安装postgresql brew install postgresql
  7. The install notes mention:
  8. 安装说明提到:

To have launchd start postgresql at login:

要在登录时启动postgresql:

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

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

Then to load postgresql now:

然后立即加载postgresql:

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

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

Or, if you don't want/need launchctl, you can just run:

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

postgres -D /usr/local/var/postgres

postgres -D / usr / local / var / postgres

  1. So run ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  2. 所以运行ln -sfv /usr/local/opt/postgresql/*.plist~/ Library / LaunchAgents
  3. Then launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  4. 然后启动加载〜/ Library / LaunchAgents / homebrew.mxcl.postgresql.plist
  5. Now re-install the gem with the Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config

    现在用Homebrew configs gem install pg重新安装gem - --with-pg-config = / usr / local / bin / pg_config

  6. Now run your rake db:create

    现在运行你的rake db:create

#5


2  

In my case this was all I needed: https://dba.stackexchange.com/a/75221

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

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

#6


1  

Also check you don't have an open connection such as a client or an application connected to the instance. If that happen you will see this in the error

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

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.

#7


1  

First, you will need to create your user manually in Postgres, while granting privilege of database creation (AKA createdb) for this user.

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

To do so, first, open Postgres shell prompt

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

$ sudo -u postgres psql

$ sudo -u postgres psql

Create your user

创建您的用户

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

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

Exit from Postgres shell

从Postgres shell退出

postgres=# \q

postgres =#\ q

Do whatever db-related rake task:

做任何与db相关的rake任务:

$ rake db:create

$ rake db:create

And it will work!

它会工作!

#8


0  

I was able to solve this by making sure that the name of my database in the database.yml file was in the following format: appname_development, appname_test, and appname_production. Here is my database.yml file as an example:

我能够通过确保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:

#1


18  

Solved!

解决了!

Solution was simple to update current installed gems.

解决方案很容易更新当前安装的宝石。

Another solution to try is set host: localhost or whatever host you use on development settings on database.yml file.

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

#2


8  

I installed PG on Mac OS X via Homebrew (I didn't realize it came default in Mac OS X), and had the same problem. I may have made the problem worse by installing the pg gem before installing pg via Homebrew.

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

I solved this same problem by doing the following:

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

  1. Make sure that all binaries are pointing to binaries in Cellar directory (/usr/local/Cellar on Mac OS X)
  2. 确保所有二进制文件都指向Cellar目录中的二进制文件(Mac OS X上的/ usr / local / Cellar)
  3. Uninstall pg gem gem uninstall pg
  4. 卸载pg gem gem uninstall pg
  5. Reinstall with Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config (if you created a rails project with pg setup, this is explained in your database.yml config file)
  6. 使用Homebrew configs重新安装gem install pg - --with-pg-config = / usr / local / bin / pg_config(如果你使用pg setup创建了一个rails项目,这在你的database.yml配置文件中有解释)

And it worked!

它奏效了!

#3


2  

I encountered an identical issue and was able to solve the error by:

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

  1. using postgresapp.com (recommended by heroku in https://devcenter.heroku.com/articles/heroku-postgresql#local-setup),
  2. 使用postgresapp.com(由heroku在https://devcenter.heroku.com/articles/heroku-postgresql#local-setup中推荐),
  3. adding to config/database.yml

    添加到config / database.yml

    host: localhost
    
  4. resetting shared memory settings as recommended in troubleshooting section of http://postgresapp.com/documentation:

    按照http://postgresapp.com/documentation的问题排查部分中的建议重置共享内存设置:

    sudo sysctl -w kern.sysv.shmall=65536
    sudo sysctl -w kern.sysv.shmmax=16777216
    

#4


2  

The answer from zquintana above worked for me with a few small caveats.

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

My solution (using Homebrew):

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

  1. If you have the gem installed gem uninstall pg
  2. 如果你有宝石安装宝石卸载pg
  3. Remove old copy of postgresql brew uninstall postgresql
  4. 删除postgresql brew卸载postgresql的旧副本
  5. Re-install postgresql brew install postgresql
  6. 重新安装postgresql brew install postgresql
  7. The install notes mention:
  8. 安装说明提到:

To have launchd start postgresql at login:

要在登录时启动postgresql:

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

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

Then to load postgresql now:

然后立即加载postgresql:

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

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

Or, if you don't want/need launchctl, you can just run:

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

postgres -D /usr/local/var/postgres

postgres -D / usr / local / var / postgres

  1. So run ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  2. 所以运行ln -sfv /usr/local/opt/postgresql/*.plist~/ Library / LaunchAgents
  3. Then launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  4. 然后启动加载〜/ Library / LaunchAgents / homebrew.mxcl.postgresql.plist
  5. Now re-install the gem with the Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config

    现在用Homebrew configs gem install pg重新安装gem - --with-pg-config = / usr / local / bin / pg_config

  6. Now run your rake db:create

    现在运行你的rake db:create

#5


2  

In my case this was all I needed: https://dba.stackexchange.com/a/75221

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

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

#6


1  

Also check you don't have an open connection such as a client or an application connected to the instance. If that happen you will see this in the error

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

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.

#7


1  

First, you will need to create your user manually in Postgres, while granting privilege of database creation (AKA createdb) for this user.

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

To do so, first, open Postgres shell prompt

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

$ sudo -u postgres psql

$ sudo -u postgres psql

Create your user

创建您的用户

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

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

Exit from Postgres shell

从Postgres shell退出

postgres=# \q

postgres =#\ q

Do whatever db-related rake task:

做任何与db相关的rake任务:

$ rake db:create

$ rake db:create

And it will work!

它会工作!

#8


0  

I was able to solve this by making sure that the name of my database in the database.yml file was in the following format: appname_development, appname_test, and appname_production. Here is my database.yml file as an example:

我能够通过确保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: