Databases2 minutes to read

How do I move an existing PostgreSQL database to Bothosting?

Rehearse a logical export into an empty hosted PostgreSQL database, check data with the application role, and plan the final transfer around live writes.

Use a logical export for a separate deployment

Move a PostgreSQL application by exporting its schema and data from the source database and importing them into an empty PostgreSQL target. Create the Bothosting database first and use its own TLS identity to connect. Logical exports are useful when database servers have separate storage and application credentials.

Inventory extensions, ownership rules and role grants before copying anything. The hosted app role owns the application database and has limited privileges. If the source relies on a privileged extension or a distinct reporting role, check that requirement before committing to the move. Use a compatible version of the dump tool. PostgreSQL’s pg_dump manual describes version constraints and exactly which objects an export includes.

Run one rehearsal on an empty target

Configure separate libpq service entries named source and target on your computer. The target entry should select the app database and role with the hostname, port and TLS files from Connection. Work in a private directory. This plain-text example suits an application whose target objects can all belong to that role.

Export application data and import into a fresh rehearsal database
umask 077
pg_dump --dbname="service=source" --no-owner --no-privileges --file=rehearsal.sql &&
  psql -X --dbname="service=target" --set=ON_ERROR_STOP=on --single-transaction --file=rehearsal.sql

Only import a dump you trust. Confirm the target is empty and disposable before running the example. If the import reports a missing extension or permission, resolve that dependency before the final transfer.

Reconcile the last writes

Read a known record by ID from both databases and compare an application-specific total. For a booking system, compare the count of active bookings for a busy date and open one booking from the app. A rehearsal export represents the source at the time it was taken. Schedule a pause for writes, make a final export, and load it into a clean target. Switch the application connection and verify a new test write on that target before reopening normal traffic. Keep the old database available for comparison until you have reconciled the results.