关于rails上的ruby:Postgres无法连接到服务器

Postgres cannot connect to server

超级难过为什么Postgres没有开发我刚刚开始的新应用程序。我已经为一个应用程序工作了。我正在使用postgres.app,它正在运行。

我开始使用新的应用程序

1
rails NEW depot -d postgresql

然后我进入了database.yml文件,并将用户名更改为我的$ USER(这是其他应用程序的工作原理)。所以现在我的database.yml文件有这个开发部分:

1
2
3
4
5
6
7
development:
  adapter: postgresql
  encoding: unicode
  DATABASE: depot_development
  pool: 5
  username: <username>
  password:

但是,当我运行"rake db:create"或"rake db:create:all"时,我仍然遇到此错误(完整,因为我不知道相关的是什么):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Couldn't create database for {"adapter"=>"postgresql","encoding"=>"unicode","database"=>"depot_development","pool"=>5,"username"=>"<username>","password"=>nil}
could not connect to server: Permission denied
    Is the server running locally and accepting
    connections on Unix domain socket"/var/pgsql_socket/.s.PGSQL.5432"?
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `initialize'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:IN `new'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:1213:in `
CONNECT'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:329:in `initialize'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:IN `new'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/postgresql_adapter.rb:28:in `
postgresql_connection'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:309:in `new_connection'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:319:IN `checkout_new_connection'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:241:in `
block (2 levels) IN checkout'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:in `loop'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:236:IN `block in checkout'
/Users/<username>/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `
mon_synchronize'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:233:in `checkout'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:96:IN `block in connection'
/Users/<username>/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `
mon_synchronize'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:404:IN `retrieve_connection'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:170:in `
retrieve_connection'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_specification.rb:144:in `connection'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/DATABASES.rake:107:IN `rescue in create_database'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:51:in `
create_database'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `block (3 levels) in <top (required)>'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/DATABASES.rake:40:IN `each'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:40:in `
block (2 levels) IN <top (required)>'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:IN `block in execute'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `
each'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:IN `block in invoke_with_call_chain'
/Users/<username>/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `
mon_synchronize'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:IN `invoke'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `
invoke_task'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:IN `each'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `
block IN top_level'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:IN `top_level'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `
block IN run'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:IN `run'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/gems/rake-0.9.2.2/bin/rake:33:in `
<top (required)>'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:in `load'

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/bin/rake:19:IN `<main>'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `
eval'
/Users/<username>/.rvm/gems/ruby-1.9.3-p194/bin/ruby_noexec_wrapper:14:in `<main>'

Couldn't create database for {"adapter"=>"postgresql","encoding"=>"unicode","database"=>"depot_test","pool"=>5,"username"=>"<username>","password"=>nil}

我试过了

1
createdb depot_development

我已经尝试进入psql环境并列出用户(其中??包括我的用户名)。在同一个psql环境中,我试过了

1
CREATE DATABASE depot;

我已经确保pg gem安装了bundle install,我运行了"pg_ctl start",我得到了这个响应:

1
pg_ctl: no DATABASE directory specified AND environment variable PGDATA unset

我跑了"ps aux | grep postgres"以确保postgres正在运行,我得到了这个回报(看起来它做得很好,对吧?):

1
2
3
4
5
6
<username>    10390   0.4  0.0  2425480    180 s000  R+    6:15PM   0:00.00 grep postgres
<username>     2907   0.0  0.0  2441604    464   ??  Ss    6:17PM   0:02.31 postgres: stats collector process      
<username>     2906   0.0  0.0  2445520   1664   ??  Ss    6:17PM   0:02.33 postgres: autovacuum launcher process      
<username>     2905   0.0  0.0  2445388    600   ??  Ss    6:17PM   0:09.25 postgres: wal writer process      
<username>     2904   0.0  0.0  2445388   1252   ??  Ss    6:17PM   0:12.08 postgres: writer process      
<username>     2902   0.0  0.0  2445388   3688   ??  S     6:17PM   0:00.54 /Applications/Postgres.app/Contents/MacOS/bin/postgres -D /Users/<username>/Library/Application Support/Postgres/var -p5432

缺点是,我一直在为WHILE进行故障排除,并且不知道出了什么问题。有任何想法吗?我真的很感激,因为我对Rails很陌生,这是一个非常令人沮丧的障碍。

谢谢!

编辑 - 每个请求,发布成功的database.yml。似乎区别在于包含密码:

1
2
3
4
5
6
7
development:
  adapter: postgresql
  encoding: unicode
  DATABASE: *******_development
  pool: 5
  username: *******
  password: *******

EDIT2 - 当我在.yml文件中添加密码,然后再次运行rake db:create时,我得到了同样的错误。我正在运行Mac OSX Lion,当我第一次安装PostgreSQL时,我做了initdb和东西。


为了创建数据库,在启动命令pg_ctl start之前

  • 你必须初始化数据库集群然后只有pg_ctl start将成功完成*

这可以通过多种方式完成
如果你使用postgres 9.1在Linux / centos上运行

service postgresql-9.1 initdb

1
service  postgresql-9.1 START

或者你可以这样做

1
2
3
4
5
6
7
8
9
$ mkdir -p /var/pgsql/DATA; chown -R postgres /var/pgsql
$ su - postgres
$ initdb -D /var/pgsql/DATA

# Starting, testing:

$ postgres -D /var/pgsql/DATA > /var/pgsql/logfile 2>&1 &
$ createdb test
$ psql test

从os到os和postgres版本初始化postgres initb差异,如果上面的命令不起作用,请看看


添加

1
host: localhost

为我的发展工作。