如何使用PostgreSQL 10.4 Docker镜像升级8.4版本数据库文件?
Absolutely! You can use your PostgreSQL 10.4 Docker image to upgrade your 8.4 database files—here's a practical, step-by-step method that’s worked for many developers:
Step 1: Validate your old 8.4 database files first
Before diving into the upgrade, confirm your existing 8.4 data directory is intact (no corruption). Spin up a temporary PostgreSQL 8.4 container to test this:
docker run -d --name pg84-temp \ -v /path/to/your/old-8.4-data:/var/lib/postgresql/data \ postgres:8.4
Wait 1-2 minutes, then check if the container stays running. If it crashes, debug with docker logs pg84-temp to fix any issues with the old data—you can’t upgrade a corrupted database. Once validated, clean up the temp container:
docker stop pg84-temp && docker rm pg84-temp
Step 2: Set up shared directories for the upgrade
You’ll need a space where both old and new data can be accessed by the upgrade process. Create local directories (or use Docker volumes) and fix permissions for the postgres user:
mkdir -p ~/pg-upgrade/old-data ~/pg-upgrade/new-data cp -r /path/to/your/old-8.4-data/* ~/pg-upgrade/old-data/ # 999 is the UID/GID of the postgres user in official PostgreSQL images sudo chown -R 999:999 ~/pg-upgrade/old-data ~/pg-upgrade/new-data
Step 3: Run the upgrade with a dual-version container
The catch: PostgreSQL’s pg_upgrade needs both old (8.4) and new (10.4) binaries. The official 10.4 image doesn’t include 8.4 tools, so we’ll use a Debian-based container that installs both versions:
docker run --rm -it \ -v ~/pg-upgrade/old-data:/old-data \ -v ~/pg-upgrade/new-data:/new-data \ debian:stretch \ bash -c " # Add PostgreSQL repo and install both versions echo 'deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main' > /etc/apt/sources.list.d/pgdg.list wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - apt-get update && apt-get install -y postgresql-8.4 postgresql-10 # Switch to postgres user to avoid permission issues su - postgres # Initialize empty 10.4 cluster initdb -D /new-data # Run the actual upgrade pg_upgrade \ --old-datadir=/old-data \ --new-datadir=/new-data \ --old-bindir=/usr/lib/postgresql/8.4/bin \ --new-bindir=/usr/lib/postgresql/10/bin "
Watch the output for errors—if everything goes well, you’ll see a success message along with post-upgrade scripts.
Step 4: Verify the upgraded database
Test the new data directory with your PostgreSQL 10.4 Docker image:
docker run -d --name pg10-upgraded \ -v ~/pg-upgrade/new-data:/var/lib/postgresql/data \ -p 5432:5432 \ postgres:10.4
Connect to the database to confirm your data is intact:
# From your local machine (if psql is installed) psql -h localhost -U postgres # Or from inside the container docker exec -it pg10-upgraded psql -U postgres
Run a few queries to check tables, data, and schema match your old database. Don’t forget to run the analyze_new_cluster.sh script generated during upgrade to refresh database statistics.
Critical Tips
- Backup first! Make a full copy of your old 8.4 data directory before starting—this is your safety net if anything goes wrong.
- Use the
--linkflag withpg_upgradeonly if you’re confident: it saves space/time by hardlinking files, but makes rolling back to the old cluster impossible. - Once you confirm the upgrade works, you can safely delete the old data directory and temporary files.
内容的提问来源于stack exchange,提问作者Steven P

