【问题标题】:Can't get PostgreSQL data to show on Heroku - Rails无法在 Heroku 上显示 PostgreSQL 数据 - Rails
【发布时间】:2017-07-28 17:47:55
【问题描述】:

我正在使用 jbuilder 创建一个 Rails API,作为我正在构建的 React 应用程序的数据库。我正在使用 PostgreSQL。我可以让 index.json.jbuilder 视图在我的本地服务器上工作:

{
 "owners": [
   {
    "first_name": "Alex",
    "last_name": "Hardy",
    "revenue": 100000,
    "audit_score": 89,
    "passing": true,
  },   ...........
{

但是这些实例不会发送到 Heroku 的服务器。我创建了 heroku 应用程序,将我的应用程序推送到 heroku (git push heroku master),并使用以下命令迁移了数据库:

heroku run rake db:migrate 

它已经存储了表格,因为当我运行时:

Heroku pg:info

我明白了:

Plan:        Hobby-dev
Status:      Available
Connections: 2/20
PG Version:  9.6.2
Created:     2017-07-26 19:09 UTC
Data Size:   7.4 MB
Tables:      3
Rows:        2/10000 (In compliance)
Fork/Follow: Unsupported
Rollback:    Unsupported
Add-on:      postgresql-contoured-63642

如果我

heroku console

并检查表 Owner (Owner.new),它显示了正确的属性(“first_name”、“last_name”....)。但是,当我访问该站点时:https://peaceful-basin-47322.herokuapp.com/owners 它只显示表格,没有该模型的任何实例:

{
    "owners": []
} 

如何将我的模型实例迁移到 Heroku?

【问题讨论】:

  • 你本地主机的数据和你heroku中的数据不同。

标签: ruby-on-rails postgresql heroku


【解决方案1】:

来自您本地主机的数据与您的 heroku 中的数据不同。 如果您希望它相同,则需要从本地主机转储您的数据,然后将其导入您的 Heroku,

首先转储你的 postgres 本地主机数据

PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump

然后将其上传到 Heroku 可以下载的 Internet,例如 amazon s3

heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL

您可以在此处阅读更多内容 https://devcenter.heroku.com/articles/heroku-postgres-import-export

rake db:migrate 只创建数据库表,不创建数据,如果你还想创建数据,你可以从seeds.rb 开始,然后rake db:seed

【讨论】:

  • 谢谢@icemelt。我不敢相信我忘了给我的数据库播种。我对你的命令有点困惑。我正在尝试执行第一个命令 to::: dump postgres localhost data::::...但是....我在哪里运行该命令?在heroku控制台中?因为如果我只是在我的目录中这样做,什么都不会发生。我过去曾成功播种本地数据库,然后运行::::: heroku run rake db:migrate,但由于某种原因,现在无法正常工作。有什么想法吗?
  • 要使 postgres 数据哑化,只需将命令放在您自己的计算机终端中即可。要导入它,请从 heroku 控制台/ heroku 运行命令运行命令
【解决方案2】:

我认为,如果您只想使用本地数据库内容填充 heroku 中的数据库,最简单的方法是在您的终端上,从您的应用文件夹:

heroku pg:info
# this will show you something like:
# === HEROKU_POSTGRESQL_RED
# Plan         Standard 0
# ...

# then with this you have the DB name HEROKU_POSTGRESQL_RED, do

heroku pg:push your_local_db_name_here HEROKU_POSTGRESQL_RED --app change_to_your_app_name

如果你的数据库不是空的,它会要求你先做pg:reset。请记住,您需要一个干净的数据库来执行此操作,如果您想将本地记录混合到现有数据库中,那么您需要以其他方式进行。

来源:https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull

【讨论】:

  • 谢谢@micael nussbaumer!你是男人!这是对我有用的第一件事。是的,我确实必须重置我的数据库,但至少它在那里,这是一个开始。我真的很感激:)!
猜你喜欢
  • 1970-01-01
  • 2017-01-28
  • 2018-12-27
  • 2013-04-19
  • 1970-01-01
  • 2014-10-13
  • 2023-03-23
  • 2014-03-30
  • 2013-04-19
相关资源
最近更新 更多