wp db import
Overview
Restore a WordPress database from a SQL dump file. The counterpart to wp db export — used to migrate sites, restore from backup, or seed development environments with real data.
What It Does
wp db import pipes a .sql (or .sql.gz) file through the mysql client using credentials from wp-config.php. It executes all SQL statements in the file against the current WordPress database.
Syntax
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--skip-optimization]
Arguments & Options
| Argument / Flag | Description |
|---|---|
FILE | Path to the SQL dump file (.sql or .sql.gz) |
--dbuser=VALUE | Override DB user |
--dbpass=VALUE | Override DB password |
--skip-optimization | Skip running OPTIMIZE TABLE after import |
Reading from stdin
You can also pipe SQL via stdin: cat dump.sql | wp db import -
Basic Usage
Import a SQL file
wp db import backup.sql
Output:
Success: Imported from 'backup.sql'.
Import a compressed file
wp db import backup.sql.gz
Import from stdin
cat backup.sql | wp db import -
Import from a remote file over SSH pipe
ssh user@prod "wp --path=/var/www/html db export --porcelain | xargs cat" | wp db import -
Complete Migration Workflow
# ── On production ──────────────────────────────────
wp db export prod_$(date +%Y%m%d).sql -- --single-transaction
scp prod_$(date +%Y%m%d).sql user@staging:/tmp/
# ── On staging ─────────────────────────────────────
cd /var/www/staging
# 1. Drop old data and recreate
wp db reset --yes
# 2. Import production dump
wp db import /tmp/prod_$(date +%Y%m%d).sql
# 3. Replace production URLs with staging URLs
wp search-replace 'https://example.com' 'https://staging.example.com' --skip-columns=guid
# 4. Flush cache
wp cache flush
# 5. Verify
wp core is-installed && echo "Migration complete ✅"
Real-World Scenarios
Scenario 1: Restore from backup after failure
# Find the most recent backup
LATEST=$(ls -t /var/backups/wp/*.sql | head -1)
echo "Restoring from: ${LATEST}"
# Reset and restore
wp db reset --yes
wp db import "${LATEST}"
# Verify
wp db check && echo "Restore verified ✅"
Scenario 2: Populate a local dev environment with real data
# Pull from staging
scp dev@staging:/var/backups/latest.sql /tmp/
# Wipe local DB and load staging data
wp db reset --yes
wp db import /tmp/latest.sql
# Replace staging URL with local dev URL
wp search-replace 'https://staging.example.com' 'http://localhost' --skip-columns=guid
# Reset admin password for local use
wp user update 1 --user_pass=admin123
Scenario 3: CI/CD test environment seeding
# In CI pipeline
wp db reset --yes
wp db import ./tests/fixtures/seed.sql
wp search-replace 'https://production.com' "http://localhost" --skip-columns=guid
Best Practices
- Always run
wp db reset --yesbefore importing into an existing site to avoid duplicate/conflicting data. - Run
wp search-replaceafter import to update URLs for the new environment. - Flush the object cache with
wp cache flushafter import. - Verify with
wp db checkto catch any import errors. - Test the import by checking the site URL and a few posts/users.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
ERROR at line X: Unknown command | Corrupted SQL file or wrong encoding | Re-export with --default-character-set=utf8mb4 |
| Slow import | Large file / no indexes | Use wpdb reset first to clear indexes; tune innodb_buffer_pool_size |
| Duplicate key errors on import | Existing data conflicts | Run wp db reset --yes first |
| URLs still point to old domain | search-replace not run | Run wp search-replace 'old.com' 'new.com' |
| Table prefix mismatch | Export from different prefix | Use wp search-replace on table prefix too |
Quick Reference
wp db import backup.sql # Basic import
wp db import backup.sql.gz # Compressed
cat backup.sql | wp db import - # Stdin
wp db reset --yes && wp db import backup.sql # Clean import
wp search-replace 'old.com' 'new.com' # Update URLs after import
Next Steps
wp search-replace— update URLs after import to a new environment.wp db check— verify the imported database is healthy.wp cache flush— clear the cache after import.wp db reset— clear existing data before importing.