Skip to main content

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 / FlagDescription
FILEPath to the SQL dump file (.sql or .sql.gz)
--dbuser=VALUEOverride DB user
--dbpass=VALUEOverride DB password
--skip-optimizationSkip 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

  1. Always run wp db reset --yes before importing into an existing site to avoid duplicate/conflicting data.
  2. Run wp search-replace after import to update URLs for the new environment.
  3. Flush the object cache with wp cache flush after import.
  4. Verify with wp db check to catch any import errors.
  5. Test the import by checking the site URL and a few posts/users.

Troubleshooting

ProblemCauseFix
ERROR at line X: Unknown commandCorrupted SQL file or wrong encodingRe-export with --default-character-set=utf8mb4
Slow importLarge file / no indexesUse wpdb reset first to clear indexes; tune innodb_buffer_pool_size
Duplicate key errors on importExisting data conflictsRun wp db reset --yes first
URLs still point to old domainsearch-replace not runRun wp search-replace 'old.com' 'new.com'
Table prefix mismatchExport from different prefixUse 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