Skip to content

Instantly share code, notes, and snippets.

@nitinhayaran
Created December 22, 2017 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitinhayaran/57a7d868fd6c8fd0f0f1b45cc8f32eeb to your computer and use it in GitHub Desktop.
Save nitinhayaran/57a7d868fd6c8fd0f0f1b45cc8f32eeb to your computer and use it in GitHub Desktop.
How to restore specific tables on heroku from backup

How to restore specific tables on heroku from backup

  1. Create a new database instance attached to current application
  2. Restore latest database backup on newly created database
  3. Get the connection url of this backup database. Use this URL in later sql queries.
  4. Connect to database where we have incorrect data
heroku pg:psql --app ck-api-prod
  1. On postgres prompt run following queries. This will create copy table from data in backup database.
CREATE EXTENSION dblink;

create table questions_copy_table
as 
SELECT * FROM dblink('postgres://XXXXX', 'SELECT id,text,description FROM questions') AS questions(id integer, text text, description text);

create table answers_copy_table
as 
SELECT * FROM dblink('postgres://XXXXX', 'SELECT id,text FROM answers') AS answers(id integer, text text);
  1. Now we will restore data from these newly created tables
UPDATE questions
SET 
text = qc.text,
description = qc.description
FROM questions_copy_table AS qc
WHERE questions.id = qc.id;

UPDATE answers
SET 
text = ac.text
FROM answers_copy_table AS ac
WHERE answers.id = ac.id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment