Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres on Heroku and dumping single table to dump file

I am using Postgres on Heroku and am needing to dump one table from my production DB and dump it into my staging DB. I have the heroku tool belt installed, but am not sure how to dump a single db table to import into my staging db.

like image 936
dennismonsewicz Avatar asked Apr 03 '13 03:04

dennismonsewicz


People also ask

How do I dump a Heroku database?

First we create a snapshot of the database on Heroku. Then we download the snapshot as 'latest. dump' (the name can be changed using -o '<name>. dump' )


3 Answers

You can dump a single table of data like so:

$ pg_dump --no-acl --no-owner -h [host ip].compute-1.amazonaws.com -U [user name] -t [table name] --data-only [database name] > table.dump

You can get all of the values needed with this:

$ heroku pg:credentials:url [DATABASE] -a [app_name]
Connection info string:
   "dbname=[database name] host=[host ip].compute-1.amazonaws.com port=5432 user=[user name] password=[password] sslmode=require"
Connection URL:
    postgres://[username]:[password]@[host ip].compute-1.amazonaws.com:5432/[database name]

This will prompt you for your password. Enter it, and you should then proceed to get a file table.dump on your local drive.

You probably want to truncate the table on staging:

$ echo "truncate [table];" | heroku pg:psql [DATABASE] -a staging_app

With that file, you can use psql with the Connection URL:output of a new call to pg:credentials for the staging app and restore just that table.

$ psql "[pasted postgres:// from pg:credentials:url of staging app]" < table.dump
SET
SET
...
...
...
...
$ 
like image 196
catsby Avatar answered Oct 08 '22 12:10

catsby


@catsbys answer

I needed to add the port as well

pg_dump --no-acl --no-owner -h [host ip].compute-1.amazonaws.com -p [port] -U [user name] -t [table name] --data-only [database name] > table.dump

like image 39
Nicolai Avatar answered Oct 08 '22 12:10

Nicolai


Take a look at taps (db:pull), your use case is covered by this answered question, I believe.

like image 20
friism Avatar answered Oct 08 '22 12:10

friism