Ruby-on-rails Postgres on Rails 致命:数据库不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17933522/
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-09-02 22:25:51  来源:igfitidea点击:

Postgres on Rails FATAL: database does not exist

ruby-on-railsruby-on-rails-3postgresqlactiverecorddatabase-migration

提问by Tyler

I've reinstalled Postgres (9.2.4) and I'm having trouble getting it set back up with Rails 3.2.11. I did:

我已经重新安装了 Postgres (9.2.4),但在使用 Rails 3.2.11 重新设置它时遇到了问题。我做了:

brew install postgresql
initdb /usr/local/var/postgres
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

So now I have

所以现在我有

$ psql --version
psql (PostgreSQL) 9.2.4
$ which psql
/usr/local/bin/psql

My database.yml file looks like

我的 database.yml 文件看起来像

development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: Tyler
  password:
  host: localhost
  port: 5432

And when I run rake db:create:allthen rake db:migrateI get the error:

而当我跑rake db:create:all,然后rake db:migrate我得到的错误:

PG::Error: ERROR:  relation "posts" does not exist
LINE 5:              WHERE a.attrelid = '"posts"'::regclass
                                    ^
:         SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                 pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
          FROM pg_attribute a LEFT JOIN pg_attrdef d
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum
         WHERE a.attrelid = '"posts"'::regclass
           AND a.attnum > 0 AND NOT a.attisdropped
         ORDER BY a.attnum

I have tried to clear out everything related to past db, migrations, etc.

我试图清除与过去的数据库、迁移等相关的所有内容。

I've deleted the schema.rb, seed.rb, and all files in the migrations folder, and anything else I can think of. But the error referring to "posts" makes me think there is still some old reference to my prior database (which had a table called "posts").

我已经删除了 schema.rb、seed.rb 和迁移文件夹中的所有文件,以及我能想到的任何其他文件。但是提到“posts”的错误让我觉得仍然有一些对我以前的数据库的旧引用(它有一个名为“posts”的表)。

Does anyone know how to troubleshoot this error, when trying to completely reinstall/refresh my database?

有谁知道如何在尝试完全重新安装/刷新我的数据库时解决此错误?

回答by user3112576

I was having a similar problem. I checked different websites and tried what they suggested but didn't work. Then I tried what you suggested. rake db:create:alland rake db:migrateit worked for me. Thank you!

我遇到了类似的问题。我检查了不同的网站并尝试了他们的建议,但没有奏效。然后我尝试了你的建议。 rake db:create:all并且rake db:migrate它对我来说有效。谢谢!

回答by user2329711

You need to create the databases first. Run rake db:create:all

您需要先创建数据库。运行 rake db:create:all

Also make sure your yml file is set up correctly for postgres.

还要确保为 postgres 正确设置了 yml 文件。

回答by N. S.

db:create:alland db:migratedid not work for me first. I changed my database name from development.pgto developmentpgin myapp/config/database.ymlfile:

db:create:alldb:migrate没有对我来说有效。从我改变了我的数据库名称development.pg,以developmentpgmyapp/config/database.yml文件中:

database: db/developmentpg

数据库:db/developmentpg

and then rake db:create:alland rake db:migrate, it worked for me.

然后rake db:create:allrake db:migrate,它对我有用。

Thank you

谢谢

回答by remi466

I tried running rake db:create:all, which didn't work. However, running bundle exec rake db:create:all worked for me.

我尝试运行 rake db:create:all,但没有用。但是,运行 bundle exec rake db:create:all 对我有用。

Hope this helps!

希望这可以帮助!