将 PostgreSQL 转储文件导入 Heroku

8
我有一个Java应用程序和postgresql数据库,它们在Heroku上运行。我可以轻松推送我的应用程序,但是数据库内容呢?我已经从数据库中导出了完整的转储文件,但是我不知道如何导入它。
通过谷歌搜索,您可以找到关于db:push的信息,这是一个受限制的rubygem,无法推送所有所需的内容。我还尝试使用heroku pg:psql --app MYAPP < db_all.out进行导入,但连接后就停止了,并且进入heroku pg:psql --app MYAPP并发出\i db_all.out会提示权限错误。
我该怎么做?
3个回答

14
您可以使用由heroku pg:credentials HEROKU_POSTGRESQL_<COLOR>提供的凭据,从本地计算机运行pg_restore命令。

5
谢谢!实际操作中需要运行以下命令:pg_restore --verbose --clean --no-acl --no-owner -h <主机名> -U <用户名> -d <数据库名> -p <端口号> <文件路径> password=<密码> - eis
5
在Postgres 9.4中,我发现需要使用以下命令:PGPASSWORD=<PASS> pg_restore --verbose --clean --no-acl --no-owner -h <HOSTNAME> -U <USER> -d <DATABASE> -p <PORT> <FILEPATH> - lollercoaster
谢谢!您能否澄清一下,我是为您的本地postgres输入数据库信息,还是为您从pg:credentials获取的heroku凭据输入?谢谢! - Coherent
@Coherent,我们在谈论Heroku数据库。 - eis

5
为了帮助那些仍然遇到此问题的人,对我而言有效的方法是使用 hgmnz 的答案,但需要进行一些修改。
更具体地说:
  1. Create a dump from the source PostgreSQL database
    $ PGPASSWORD=YOUR_PG_PASSWORD pg_dump -Fc --no-acl --no-owner -h localhost -U YOUR_PG_USER YOUR_DB_NAME > YOUR_DB_NAME.dump
    
  2. Get the Heroku Postgres credentials for your heroku app
    $ heroku pg:credentials:url -a YOUR_APP_NAME
    
  3. Attempt to import PostgreSQL dump to Heroku using the credentials above
    $ pg_restore --verbose --clean --no-acl --no-owner -h HOSTNAME -U USER -d DATABASE -p PORT PATH/TO/YOUR_DB_NAME.dump --password
    
  4. Enter the password received from the Heroku Postgres credentials
  5. It should then import the dump successfully

1
这很简单,对我很有效:
heroku pg:psql -a {YOUR_APP} -f {YOUR_DUMP_PATH}
我认为这比使用标准输入语法(如OP示例中所示)更好,因为它使用Heroku命令本身提供的选项。
在提交之前,您可能需要检查转储文件是否正确(在我的情况下不是这样)。

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