postgresql 致命:数据库“postgres”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10860288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:58:21  来源:igfitidea点击:

FATAL: database "postgres" does not exist

ruby-on-railspostgresql

提问by user1432031

Weird error. All help appreciated.

奇怪的错误。所有帮助表示赞赏。

Here's my database.yml

这是我的 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:

When I create the databases manually and try to use them, for example rake db:test:preareI get the following error:

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

FATAL:  database "postgres" does not exist

When I try to drop the databases I get the following errors:

当我尝试删除数据库时,出现以下错误:

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

If I try to create the databases through with rake db:create:allI get the following errors:

如果我尝试通过 with 创建数据库,rake db:create:all则会收到以下错误:

app_development already exists
app_test already exists
app_production already exists

So it appears that my database.yml is ok. But for some reason it's looking for a database called postgres when that's not what's in my database.yml.

所以看起来我的database.yml 没问题。但是由于某种原因,它正在寻找一个名为 postgres 的数据库,而它不是我的 database.yml 中的内容。

Any help appreciated.

任何帮助表示赞赏。

EDIT:

编辑:

Here is more from the trace of rake:

这是更多来自耙子的痕迹:

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

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

采纳答案by Richard Huxton

You've not said whether you actually have a postgres database, which is what the error is complaining about.

你没有说你是否真的有一个 postgres 数据库,这就是错误所抱怨的。

Presumably not, and this "rake" tool is connecting as user postgres to database postgres so it can create the new databases. Obviously it needs to connect as someone to do anything.

大概不是,这个“rake”工具以用户 postgres 的身份连接到数据库 postgres,以便它可以创建新的数据库。显然,它需要以某人的身份进行连接才能做任何事情。

Do take the time to skim through the PostgreSQL manuals and understand how to enable/control logging. It'll serve you well in the long run. I'm guessing there is a debug/log setting for rake/rails too.

请花时间浏览 PostgreSQL 手册并了解如何启用/控制日志记录。从长远来看,它会为您提供良好的服务。我猜 rake/rails 也有调试/日志设置。

回答by Flavio Wuensche

[Mac OS X] I've never had any problem with PostGreSQL while using Linux. However, I started working with a MacBook and got the same error as you. So... Probably this is not the solution you were expecting for, but this one app solved all my headaches. I just wanted to share it with you guys.

[Mac OS X] 我在使用 Linux 时从未遇到过 PostGreSQL 问题。但是,我开始使用 MacBook 并遇到与您相同的错误。所以... 可能这不是您期望的解决方案,但是这个应用程序解决了我所有的头疼问题。我只是想和大家分享一下。

http://postgresapp.com/

http://postgresapp.com/

回答by Kaygi22

I'm using Mac OS X, I had the same problem with python and sqlalchemy. Just like Flavio suggested, check postgresapp and check How to connect section.

我使用的是 Mac OS X,我在使用 python 和 sqlalchemy 时遇到了同样的问题。就像 Flavio 建议的那样,检查 postgresapp 并检查如何连接部分。

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