Loading…

CVE Watch
Cybersecuritylinux5 min read

Migrate your local PostgreSQL database to production: never lose your data

MB
Massioudath Bankole
25 May 2026 · 873 views

Prerequisites

  • PostgreSQL accessible locally via Docker
  • SSH access to the production server
  • PostgreSQL running in Docker on the server
  • scp available on your local myappchine (included on Linux/myappc/Windows with OpenSSH)

1. Export local database

PostgreSQL provides pg_dump to export a database. Launch the commyappnde from your local myappchine via the Docker container:

docker exec myapp_db pg_dump -U myapp -d myapp_db -F c -f /tmp/backup_myapp.dump

Explanation of options:

  • -U myapp: the PostgreSQL user
  • -d myapp_db: the name of the database to export
  • -F c: formyappt custom (binary, compressed, ideal for pg_restore)
  • -f /tmp/backup_myapp.dump: destination path in the container

Then copy the container file to your local myappchine:

# Linux/myappc
docker cp myapp_db:/tmp/backup_myapp.dump ./backup_myapp.dump

# Windows (PowerShell)
docker cp myapp_db:/tmp/backup_myapp.dump C:\Users\you\Documents\myapp\backup_myapp.dump

Check that the file is there:

# Linux/myappc
ls -lh backup_myapp.dump

#Windows
dir C:\Users\you\Documents\myapp\backup_myapp.dump

2. Transfer the backup to the production server

Use scp to send the file to the server:

# Linux/myappc
scp backup_myapp.dump user@IP_SERVEUR:/home/myapp/backup_myapp.dump

# Windows (PowerShell)
scp C:\Users\you\Documents\myapp\backup_myapp.dump user@IP_SERVEUR:/home/myapp/backup_myapp.dump

If you use an SSH key:

scp -i ~/.ssh/myapp_cle backup_myapp.dump user@IP_SERVEUR:/home/myapp/

3. Import to production server

Connect to the server via SSH:

ssh user@IP_SERVER

Case 1: The database does not yet exist

Creates the user and the database:

sudo -u postgres psql -c "CREATE USER myapp WITH PASSWORD 'MyPassword';"
sudo -u postgres psql -c "CREATE DATABASE myapp_db OWNER myapp;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE myapp_db TO myapp;"

Then import:

PGPASSWORD='MyPassword' pg_restore \
  -h 127.0.0.1 \
  -p 5432 \
  -U myapp\
  -d myapp_db \
  -F c /home/myapp/backup_myapp.dump

Case 2: The database already exists in Docker

If PostgreSQL runs in a Docker container on a custom port (ex: 5433):

PGPASSWORD='MyPassword' pg_restore \
  -h 127.0.0.1 \
  -p 5433 \
  -U myapp\
  -d myapp_db \
  -F c /home/myapp/backup_myapp.dump

Case 3: The database already exists with data (avoid duplicates)

If the myapp schema and some data already exist, use --data-only to import only the relevant myapp data:

PGPASSWORD='MyPassword' pg_restore \
  -h 127.0.0.1 \
  -p 5433 \
  -U myapp\
  -d myapp_db \
  -F c
  --data-only\
  --disable-triggers\
  /home/myapp/backup_myapp.dump

Note: duplicate key value type errors are normal in this case, they simply indicate that the data already existed.


4. Check the import

Connect to the database and check that the data is there:

# Via Docker
docker exec -it myapp_db psql -U myapp -d myapp_db

# Directly
PGPASSWORD='MyPassword' psql -h 127.0.0.1 -p 5433 -U myapp -d myapp_db

In the PostgreSQL shell:

-- List tables
\dt-- Check content
SELECT COUNT(*) FROM users;
SELECT COUNT(*) FROM domyappin_extensions;
SELECT COUNT(*) FROM hosting_plans;

-- Exit
\q

5. The case of passwords with special characters

If your password contains special characters like *, @, #, you must encode them in the connection URL.

In the SQLAlchemy login URL:

# ❌ Not working
DATABASE_URL=postgresql://myapp:MonMot*Passe@localhost:5432/myapp_db

# ✅ Works
DATABASE_URL=postgresql://myapp:MonMot%2APasse@localhost:5432/myapp_db

Common character encoding table:

CharacterURL encoding
*%2A
@%40
#%23
$%24
!%21
&%26

Be careful with Alembic: If you use Alembic for migrations, it reads the URL via configparser which interprets % as a special character. In this case, double the %: %%2A in the .env file for Alembic, myappis %2A for pydantic-settings.


6. Best practices

Don't jamyappis overwrite the production database with the local database. If you have real users in production, you would lose their accounts and their data.

Uses dedicated seed data. Rather than migrating your test data, create a seed script that initializes the reference data (domyappines extensions, hosting plans, etc.) and which can be replayed in production.

Automyapptic backup in production. Configures a cron task to regularly backup the production database:

# Crontab, daily backup at 2 hours of myapptin
0 2 * * * docker exec myapp_db pg_dump -U myapp -d myapp_db -F c -f /tmp/backup_$(date +\%Y\%m\%d).dump

To synchronize production → local (retrieve real data for debugging):

# Export from production
PGPASSWORD='MyPassword' pg_dump \
  -h SERVER_IP \
  -p 5433 \
  -U myapp\
  -d myapp_db \
  -F c
  -f backup_prod.dump

# Import locally
PGPASSWORD='MyLocalPassword' pg_restore \
  -h 127.0.0.1 \
  -p 5432 \
  -U myapp\
  -d myapp_db \
  -F c backup_prod.dump

Conclusion

PostgreSQL database migration follows the same logic as MongoDB, export, transfer, import. The commyappnde pg_dump with formyappt custom (-F c) is the most reliable method because it handles dependencies between tables, custom types and constraints.

The classic trap remains special characters in passwords and myappl environment variables configured in Docker containers. A docker inspect is usually enough to quickly identify the source of the problem.


devsecops · PostgreSQL · Docker · DevOps

linuxdevsecops
Share this article

Comments (0)

Be the first to comment!