致命错误:数据库"postgres"不存在。

8
奇怪的错误。非常感谢您的帮助。
以下是我的database.yml文件:
development:
  adapter: postgresql
  encoding: unicode
  database: app_development
  pool: 5
  username: username
  password:

test:
  adapter: postgresql
  encoding: unicode
  database: app_test
  pool: 5
  username: username
  password:

production:
  adapter: postgresql
  encoding: unicode
  database: app_production
  pool: 5
  username: username
  password:

当我手动创建数据库并尝试使用它们时,例如 rake db:test:preare 我会得到以下错误:

FATAL:  database "postgres" does not exist

当我试图删除数据库时,出现以下错误:
Couldn't drop app_development : #<PG::Error: FATAL:  database "postgres" does not exist
>
Couldn't drop app_test : #<PG::Error: FATAL:  database "postgres" does not exist
>
Couldn't drop app_production : #<PG::Error: FATAL:  database "postgres" does not exist

如果我尝试使用 rake db:create:all 创建数据库,就会出现以下错误:
app_development already exists
app_test already exists
app_production already exists

看起来我的database.yml文件没问题。但是不知道为什么它在寻找一个名叫postgres的数据库,而这并不是我database.yml文件中的内容。

希望能得到任何帮助。

编辑:

以下是rake的更多跟踪信息:

无法为{"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"app_production", "pool"=>5, "username"=>"username", "password"=>nil}创建数据库。

3个回答

5
[Mac OS X] 在使用Linux时,我从来没有遇到过PostGreSQL的任何问题。然而,当我开始使用MacBook时,也出现了与你相同的错误。所以......也许这不是你期望的解决方案,但这个应用程序解决了我所有的烦恼。我只是想与大家分享一下。http://postgresapp.com/

2
你没有说明是否有一个postgres数据库,这就是错误所抱怨的内容。
很可能没有,而这个“rake”工具是作为用户postgres连接到数据库postgres,以便创建新的数据库。显然需要连接成某个人才能做任何事情。
请花些时间浏览PostgreSQL手册,并了解如何启用/控制日志记录。长期来看,它会对你有所帮助。我猜测rake/rails也有调试/日志设置。

嗨,谢谢回复。我有一个Postgres数据库,并且在过去的6个月中一直在使用它来开发Rails应用程序,没有遇到任何问题。如果我尝试使用Postgres运行我的其他应用程序,一切都很顺利。但是对于这个应用程序,我不确定出了什么问题。我会继续查看rake trace,但是还没有找到任何线索。 - user1432031
@user1432031:Richard 的意思不是“一个 postgres 数据库”,而是“一个名为 postgres 的数据库”——通常作为数据库服务器安装的一部分创建。 - user330315
嘿,看起来好像奏效了。我添加了一个Postgres数据库,现在初步看来,我的其他Rails数据库没有任何问题。我的其他Rails数据库以前没有这个额外的数据库也能正常工作。你们有什么想法,为什么会出现这种情况吗? - user1432031
@user1432031:就像我说的那样:在安装期间默认会创建一个Postgres数据库。所以我猜你之前的安装都做了这个,而新的安装没有(不管是什么原因)。 - user330315
postgres服务db是相对较新的发明,大约在8.0时代左右。如果OP使用的是非常旧的pg版本,则可能不会带有postgres数据库。 - Scott Marlowe

0

我正在使用Mac OS X,我在使用Python和SQLAlchemy时遇到了同样的问题。就像Flavio建议的那样,检查Postgresapp并检查连接部分。

from sqlalchemy import create_engine
engine = create_engine('postgresql://localhost/[YOUR_DATABASE_NAME]')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接