Skip to content

Instantly share code, notes, and snippets.

@ddrscott
Last active January 5, 2018 15:32
Show Gist options
  • Select an option

  • Save ddrscott/8eed79573192d982486d7e43e3f01211 to your computer and use it in GitHub Desktop.

Select an option

Save ddrscott/8eed79573192d982486d7e43e3f01211 to your computer and use it in GitHub Desktop.
Export specific records from Postgres and import into another DB in a different docker host.
# export source data to file
psql anon -c "copy (SELECT * FROM assets WHERE color='red') to stdout csv header" > assets.csv
# create new database instance (without password)
docker run --name postgres-9.6 -e POSTGRES_PASSWORD='' -d -p 65436:5432 postgres:9.6
# copy table screma
pg_dump --schema-only --table=assets anon > schema.sql
# might need to remove foreign keys
$EDITOR schema.sql
# create new schema
psql -h localhost -p 65436 -U postgres < schema.sql
# insert into destination DB
psql -h localhost -p 65436 -U postgres -c 'copy assets from stdin with (format csv, header)' < assets.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment