Export a Heroku Postgres Schema

2015-09-10 13:58:45 -0400

This is a quick script to export just the schema from a Heroku Postgres database. It exports the schema with no ownership information, so you can easily load it into a local database.

#!/usr/bin/env ruby
require 'uri'
db = URI(`heroku config:get DATABASE_URL`)
system("export PGPASSWORD=#{db.password}; " +
       "pg_dump --schema-only -h #{db.host} -p #{db.port} " +
       "-U #{db.user} -d #{db.path.gsub('/','')} --no-owner -f schema.sql")

Make sure you run this script from a folder within your app (with the Heroku git remote correctly set up). It will put the schema in schema.sql in the current working directory.