Transitioning PostgreSQL From Docker on K8s to Standalone PostgreSQL
This document describes how to migrate from Docker PostgreSQL to standalone PostgreSQL with existing data.
Last updated
This document describes how to migrate from Docker PostgreSQL to standalone PostgreSQL with existing data.
Last updated
Copyright © 2024 OpenG2P. This work is licensed under Creative Commons Attribution International LicenseCC-BY-4.0 unless otherwise noted.
Migrating PostgreSQL from a Kubernetes cluster (running in Docker containers with NFS storage) to an external standalone instance is a strategic move for improving scalability, performance, and manageability in production. Below is a step-by-step guide to performing this transition with minimal disruption.
Pre-Migration Planning:
Downtime Requirements: Decide if you need minimal downtime (consider replication if necessary).
Networking: Verify that the new PostgreSQL instance is accessible, and configure firewalls or security groups.
Scaling: Ensure the new instance has enough CPU, memory, and storage for future needs.
Create a Backup of the Current Database.
Follow the document here to Access a Database from Outside the Cluster.
To back up a PostgreSQL database using pg_dump
, you can use the following command structure:
Example:
This command connects to a PostgreSQL database and creates a dump of the specified database, saving it to a file.
Restore the Backup to the External Instance.
Copy the db.dump
file into your newly created PostgreSQL instance, and run the following command to restore the backup data into the empty database created in step 2. Ensure that the database is created before running the restore command.
To restore a database from a dump file, use the following psql
command:
Example for a specific user and database:
Then verify whether all the tables were created successfully after the restore.
Update Application to Use the External Instance.
Go to the Rancher UI, select Apps -> Installed Apps, and choose the service you want to edit or upgrade. Edit the YAML file and update the following parameters to connect to the new external database.
Once you are done with the edit/upgrade, the Odoo service might fail to connect to the externally configured database and display an error message indicating that it is unable to connect to the database. This occurs because the service will attempt to connect to the previous database host. This happens due to the post-init-openg2p.sh
script, which creates the odoo.conf
file only once during deployment.
To resolve this issue, you can exec into the pod and run the following command to update the odoo.conf
file with the external database details:
After running this command, restart the Odoo service. Once it is up and running, exec into the pod again and verify whether the external database details have been updated in the odoo.conf
file located in the directory /bitnami/odoo/conf/odoo.conf
.
Ensure Data Consistency after the migration.
Data Verification: Use tools such as pg_dump
or custom SQL queries to compare the data in the old and new databases. Ensure that no data was missed during the migration.
Check Application Performance: Monitor how the application performs with the new PostgreSQL instance. Ensure that query performance is optimal, and database latency is within acceptable limits.