2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ローカルのPostgreSQLをHerokuPostgresにリストアしようとして失敗し続けた

Last updated at Posted at 2019-12-24

SQLを全然覚えてないから全部「DBeaver」任せでデータベースをいじってます。
CUIクライアント最高~!

と、そんな中、ローカルのPostgreSQLデータベースを、HerokuPostgreSQLにインポートしようと思ったら、かなり手こずったので記録に残します。

インポート手順

HerokuPostgreSQLの導入と、ローカルからのインポートに関しては、
こちらの記事が参考になります。
Heroku Postgres を利用してみよう
Herokuの公式ドキュメント

つまった個所

heroku pg:backups:restore '<SIGNED URL>' DATABASE_URL

上記のリストアを試みたときにおこったエラー達。

ちなみにDATABASE_URLは実際のURLを使わず、そのままでも可能。
(一応、URLは「heroku config」で確認できます。)

バックアップが見つからない

エラー内容

 !    Backup 'dumpファイルをアップロードしてる場所のURL' not found for 
 !    アプリ名

解決法

Windowsではダブルクオーテーションを使う。
参考

読み込み時エラー①

エラー内容

pg_restore -F c -d dbname dump.backup
pg_restore: [archiver] did not find magic string in file header

不適切な形式でdumpファイルが作成されているときに表示されるエラーらしい。
詳しくはこちら

解決法

PowerShellを使わない。

不適切なdumpファイルということには、思い当たる節はありました。
というのもpg_dumpを行った際に、下記の警告がたくさん表示されていたので…

pg_dump: [カスタムアーカイバ] 警告: ftellで想定位置との不整合がありました -- ftellが使用されました

PowerShellでは、PostgreSQLのpg_dumpやpg_restoreは行えないようです。
参考

読み込み時エラー②

エラー内容

Restoring... !
 !    An error occurred and the backup did not finish.
 !
 !    pg_restore: [custom archiver] could not read from input file: end of file
 !    pg_restore finished with errors
 !    waiting for download to complete
 !    download finished with errors
 !    please check the source URL and ensure it is publicly accessible
 !
 !    Run heroku pg:backups:info r007 for more details.

解決法

分かりませんでした。

「pg_restore: [custom archiver] could not read from input file: end of file」って
ググって出てきたものを色々試してみたのですが、どうにもなりませんでした。

分かる方がいれば、教えていただけますとありがたいです。
「end of file」だから、おそらく改行コード周りっぽいんですが辿り着けず…

実際にリストアできた手順

こちらの下記の手順でリストア自体は行えました。 [https://stackoverflow.com/questions/36240600/cant-pg-restore-on-heroku-could-not-read-from-input-file-end-of-file](https://stackoverflow.com/questions/36240600/cant-pg-restore-on-heroku-could-not-read-from-input-file-end-of-file)

コメント 2019-12-24 173804.png

カレントディレクトリ(リポジトリ)に作成したdumpファイルをそのままロードしたら行けた…
Amazon S3もGoogleドライブも何も使わずいけちゃっていいの…?

更新する都度、データベースをリセットして最新のdumpファイルを当てていく。

heroku pg:reset DATABASE
pg_dump --no-owner -U ユーザー名 データベース名 > mydb.dump
heroku pg:psql < mydb.dump
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?