Updated: 12 November 2026 · Applies to: WordPress 7.1.4, WP-CLI 2.12.0, MariaDB 11.8 (Debian 13)
To import an SQL file into a WordPress database with WP-CLI, go to the WordPress folder (the one that holds wp-config.php) and run:
wp db import backup.sql
WP-CLI reads the database name, user and password from wp-config.php, so you do not type them. The steps below add a safety backup, compressed files, a different table prefix and a changed site address.
Before you start
- You need SSH or the cPanel Terminal, and WP-CLI installed. See How to install WP-CLI in WordPress if
wp --versiongives "command not found". - The database named in
wp-config.phpmust already exist.wp db importonly runs the SQL in the file; it does not create the database. - Run WP-CLI as the site owner, not as root, so new files keep the right owner.
Import a database step by step
- Upload the
.sqlfile to your home folder with File Manager, SFTP orscp. - Go to the WordPress folder:
cd ~/public_html
- Back up the current database first, so you can go back if the import is wrong:
wp db export ~/before-import.sql
Output:
Success: Exported to '/home/deploy/before-import.sql'.
More options for this step are in How to Back Up a WordPress Database with WP-CLI (wp db export).
- Import the file:
wp db import ~/backup.sql
Output:
Success: Imported from '/home/deploy/backup.sql'.
- Check that WordPress reads the imported data, for example the site title:
wp option get blogname
- Check the tables for errors:
wp db check
A dump made with wp db export or mysqldump contains DROP TABLE IF EXISTS for each table, so the import replaces those tables. Tables that are in your database but not in the file stay as they were.
Start from an empty database
To make the database match the file exactly (no leftover tables from plugins you removed), empty it first. This deletes every table in the database, so take the backup in step 3 before you run it:
wp db reset --yes
wp db import ~/backup.sql
Import a compressed .sql.gz file
wp db import cannot read a gzip file directly. In our test it stopped with:
ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mode is enabled and mysql is run in non-interactive mode.
Unpack it on the fly and pass - so WP-CLI reads from standard input:
gunzip < ~/backup.sql.gz | wp db import -
Output:
Success: Imported from 'STDIN'.
"The site you have requested is not installed" after the import
This means the tables in the file use a different prefix from the one in wp-config.php. WP-CLI tells you which one it found:
Error: The site you have requested is not installed. Your table prefix is 'wp_'. Found installation with table prefix: abc_.
Set the prefix in wp-config.php to match the imported tables:
wp config set table_prefix abc_
Then list the tables WordPress now uses:
wp db tables
Moved to a new domain? Update the URLs
If the file came from a site on another address, replace the old URL everywhere in the database. Run it with --dry-run first to see how many changes it would make:
wp search-replace 'https://old.example.com' 'https://new.example.com' --skip-columns=guid --dry-run
If the numbers look right, run it again without --dry-run. wp search-replace also fixes URLs inside serialised PHP data (widgets, theme settings), which a plain SQL REPLACE would break. How to Change the WordPress URL in the Database covers the other methods.
Common errors
- Import file missing or not readable: the path is wrong, or the file belongs to another user. Use the full path, for example
~/backup.sql. - Access denied for user: the database user in
wp-config.phphas no rights on that database. In cPanel, add the user to the database with all privileges (How to add a user to a database and add privileges). - Error establishing a database connection in the browser after the import: see How to fix Error Establishing a Database Connection in WordPress.
No SSH access? You can import the same file in phpMyAdmin instead: Import a Database in phpMyAdmin: Steps and Common Errors.
Official documentation: wp db import and wp search-replace on developer.wordpress.org.