Skip to content

Database copy to save cost

Leo Leung edited this page Oct 18, 2024 · 2 revisions

Background

Instance type of aws rds database can be changed, but the storage can only be increased not decreased. Therefore, we need to copy data from old database to new database.

Instructions

  1. Create snapshot from old database.
  2. Use replace in terraform to create new database with smaller storage.
  3. Create an instance server from old database snapshot for data migration.
  4. Make sure you are in the network that can connect both old database snapshot server and new database.
  5. Install psql and pg_dump commands. Make sure the database tool same version as the remote database. Refer to this installation guide https://www.atlantic.net/dedicated-server-hosting/how-to-install-and-configure-postgres-14-on-ubuntu/ .
  6. Run postgresql commands. (both databases share same password)
    export PGPASSWORD=
    pg_dump -C -h old_hostname -U old_user old_db_name | psql -h new_hostname -U new_user new_db_name
    
Clone this wiki locally