Search code examples
postgresqlherokubackuppg

Custom pg:dump options with Heroku pg:backups capture?


When developing, I need to pull the latest database so I know I'm working with the latest data. However, we keep a table full of Archives that I don't need to bother downloading because it's a very large table.

I know pg_dump allows for custom parameters that will let you exclude a certain table from being dumped.

Without doing anything crazy like having 2 databases, 1 for data and 1 for archives, is there any way to download everything BUT the archives table from Heroku?

I still need it to keep backups of the archives table, but I don't want to be downloading it. Can I just do a pg_dump when needed that is seperate from the backups?

I know it's a long shot, but any suggestions would be greatly appreciated.


Solution

  • You can't add any custom pg_dump options when using heroku pg:backups capture. This command actually calls an undocumented Heroku Postgres API and it doesn't pass any parameters (see here for the code if you are curious).

    What you can do is run your own pg_dump dump command that points to the Heroku Postgres instance.

    Get the connection info with pg:credentials where DATABASE_URL can also be the the database color if you have more than one database attached to the app:

    > heroku pg:credentials DATABASE_URL --app app_name
    Connection info string:
       "dbname=zzxcasdqwe host=ec2-1-1-1-1.compute-1.amazonaws.com port=1111 user=asdfasdf password=qwertyqwerty sslmode=require"
    Connection URL:
       postgres://asdfasdf:[email protected]:1111/zzxcasdqwe
    

    Take either the the connection info string or the connection url and include that as the first argument to pg_dump and add your custom options

    pg_dump "dbname=zzxcasdqwe host=ec2-1-1-1-1.compute-1.amazonaws.com port=1111 user=asdfasdf password=qwertyqwerty sslmode=require"\
            -n schema -t table -O -x -Fc -f dump.out
    # OR
    pg_dump postgres://asdfasdf:[email protected]:1111/zzxcasdqwe \
            -n schema -t table -O -x -Fc -f dump.out
    

    I also co-wrote a Heroku plugin (parse_db_url) that will parse DATABASE_URL's into other formats like pg_dump, pg_restore, pgpass etc. I find it useful when dealing with several different Heroku databases.