To get a backup SQL file, you export your database as a plain-text dump of SQL statements, usually with a command-line tool such as mysqldump, pg_dump, or sqlite3 .dump, or through your host's control panel. The result is a .sql file containing the statements needed to rebuild your schema and data. The one condition that matters most: you need an account with read access to every table, and enough disk space to hold the output. The exact command depends on which database engine you run, how large the database is, and whether you want schema only, data only, or both.
This guide covers the practical paths for the four engines you'll meet most often—MySQL/MariaDB, PostgreSQL, SQLite, and Microsoft SQL Server—plus what to do when the command line isn't available, how to verify the file, and where backups usually go wrong.
Before You Export: Access, Space, and a Safe Output Location
Every method below assumes three prerequisites, so settle them first.
Credentials with sufficient privileges. In MySQL you need SELECT, SHOW VIEW, TRIGGER, and (for a fully consistent dump) RELOAD or the LOCK TABLES privilege. In PostgreSQL, dump privileges follow the user's read access to the tables, views, and sequences; a superuser export is simpler but not always allowed. In SQL Server, the account needs read access plus the right to back up the database.
Free disk space at the destination. The dump is roughly the size of your data plus the space the SQL text and indexes need; indexes and logs can multiply that. A rough sanity check: compare the dump size afterward against the database's reported size—if the dump is a tiny fraction of the data size, you probably grabbed schema only.
A destination you control and can write to. Avoid dumping straight onto the same failing disk or the same server volume you're trying to protect against. Write to a mounted backup drive, network share, or object-storage sync folder, and confirm afterward that the file actually landed.
If any of these are missing—your account lacks privileges, the backup target can't hold the output, or the database lives on managed hosting that restricts shell access—fall back to the control-panel or managed-service export described later.
Exporting a MySQL or MariaDB Database
mysqldump ships with MySQL and MariaDB server and client installs and is the standard way to produce a SQL file. Run it from a shell on the database host or from any machine with client tools and network access.
Single database, schema and data:
All databases on the server:
Schema only or data only: add --no-data to keep just the structure, or --no-create-info to skip table definitions and keep rows.
One or a few tables: list them after the database name, mydb orders customers.
The flags that matter for correctness: --single-transaction takes a consistent snapshot inside one transaction for transactional tables such as InnoDB, without locking writers out; it doesn't help for MyISAM tables, which need a lock. --routines and --triggers include stored procedures, functions, and triggers that a plain dump omits by default. If your data includes binary columns, --hex-blob keeps the dump text-safe.
What to expect: a large plain-text file, possibly several gigabytes, and the shell prompt returning with no output when it finishes. How to check success: look for the file, confirm it ends without an error message, and run a quick inspection as described in the verification section.
Two common failures. First, "Access denied" errors mid-dump usually mean a routine or view references objects the backup user can't read—see the privileges above. Second, if the shell prints a "Warning: Using a password on the command line interface can be insecure" message, that's harmless, but typing the password when prompted (rather than as -pSecret) is cleaner.
Once you've got the command working, the same invocation becomes your repeatable backup recipe. Wrap it in a script or cron job, add a timestamp to the filename, and keep the last several files.
Exporting a PostgreSQL Database
pg_dump is PostgreSQL's equivalent, and it produces a .sql file by default in plain-text format.
Plain SQL for one database:
-F p selects plain text, which is what you want if the destination must be a .sql file you can read and edit. pg_dump also offers -F c (a compressed custom-format archive, not plain SQL, restorable with pg_restore) and -F d (a directory of files). For a genuine SQL file, use -F p.
All databases: pg_dump handles one database at a time, so back up the whole cluster with pg_dumpall -U backup_user -f cluster_2026-11-13.sql, which includes roles and tablespaces but not per-database encodings or ownership differences.
Schema or data only: --schema-only and --data-only do what they say. For selective restores, -t orders dumps one table.
A note on consistency: pg_dump takes a consistent snapshot by itself as long as it's connecting to a running server and the server can serialize the snapshot. It doesn't block writes.
What to expect and how to check: as with MySQL, watch for the error stream—pg_dump reports errors separately and may still produce a partial file. A non-zero exit code means trouble even if the file exists. Confirm the file is non-empty and ends with a statement terminator or comment near the end.
One practical wrinkle: large-object data and blobs are included, but if the destination directory or your shell environment truncates filenames or output, you can get a silently incomplete dump. Always check file size against expectations.
Exporting a SQLite Database
SQLite is a single file, and its .dump command prints the SQL needed to recreate the whole database—schema, data, indexes, triggers. That's the cleanest way to get a portable SQL file from it.
From the sqlite3 shell:
From the command line in one step, same idea:
Because SQLite reads from a file rather than a server, you can copy the .db file directly if the database is idle; if writers may be active, .dump or the .backup command gives a consistent snapshot. VACUUM INTO also produces a clean, compact copy, but that's a database file, not SQL text.
What to expect: a plain-text script, typically much smaller than the database file, that runs against an empty database. How to check success: run it back into a scratch database as described below. Beware the classic pitfall—for very large databases, .dump output piped to a text file works, but a shell that inserts carriage returns or mangles binary content can corrupt the result; pipe through a binary-safe redirect.
Exporting Microsoft SQL Server Database
SQL Server's native backup produces a .bak binary file, not a .sql file. If the goal is text SQL, you'll use a scripting tool instead.
Generate SQL scripts via SSMS: connect to the instance, right-click the database, and use Tasks, then Generate Scripts. Choose "Schema and data" and a single file target, and you get an executable .sql file with CREATE and INSERT statements. This is the conventional way to pull a .sql file out of SQL Server.
From the command line, sqlcmd can dump query results to a file, but it doesn't script a whole database's objects. For automated scripting, use mssql-scripter on a client machine against the server.
The .bak route, for true backups: use BACKUP DATABASE in T-SQL or SSMS's Tasks, Back Up. That file restores cleanly with RESTORE DATABASE, but it's binary and version-bound—a .bak from a newer major version won't restore onto an older server.
Choose based on what "backup SQL file" means in your context: portable text that any SQL Server can replay, or an exact engine-level backup. Most people asking for a .sql file want the former.
Getting a Dump Without Shell Access: Admin Panels and GUI Tools
Not everyone can run a terminal command against their database. Two common routes help here.
Hosting control panels. Many shared and managed hosts expose phpMyAdmin (for MySQL/MariaDB) or a comparable tool for PostgreSQL. In phpMyAdmin, select the database, open the Export tab, choose the SQL format, and click Go; the browser downloads a .sql file. In a PostgreSQL admin tool, the export option is usually labeled as a plain-text or SQL dump. The export dialog may default to "schema only" or "structure and data"—pick the right one, since an export that omits the data is the single most frequent mistake in this route.
Desktop GUI clients. Tools that connect to your database and offer "dump" or "export" menu commands produce the same files the command-line tools do. They're useful when a colleague needs to hand you a project's backup without teaching you the engine's CLI.
Managed cloud services. If your database runs on a managed platform, the platform's own backup feature often creates engine-native files. Whether you can download them as .sql depends on the service—check its documentation. Where downloads are limited, the GUI or client dump approach is the fallback.
In every case, the success check is the same: confirm the downloaded or saved file exists, has a plausible size, and opens as text.
What the File Should Contain, and How to Restore It
A complete SQL backup file includes, in order: statements to drop or create the database (depending on the tool's flags), CREATE TABLE statements for every table, INSERT statements or a bulk-load sequence for the rows, and then indexes, constraints, views, triggers, and routines. If any of those are missing, your file is incomplete.
Verify with a test restore into a scratch database. For MySQL, create an empty database and load the file:
For PostgreSQL, createdb scratch_db then psql -d scratch_db -f appdb_2026-11-13.sql. For SQLite, sqlite3 scratch.db < mydata_2026-11-13.sql. For SQL Server, run the script against an empty test database in SSMS.
Then run a few counts in the restored copy—SELECT COUNT(*) per table—and compare them with the source. Exact counts are the fastest evidence that the dump carried the data, not just the structure.
A simple arithmetic check. If your table reports 1,200,000 rows and the restored copy also reports 1,200,000, the data moved. If the dump was taken while users were still writing, small differences in row counts and recent values are expected; a consistent-snapshot flag like --single-transaction or pg_dump eliminates most of that drift.
Stop condition. If the dump errors repeatedly on a specific routine, view, or table—typically a permissions or corruption symptom—stop fiddling with flags and get help from whoever administers the database. And if the database is production and you can't get a consistent snapshot, the safer path is a native backup plus a maintenance window rather than an ad-hoc dump.
Common Failures and How to Read Them
"Access denied" or "permission denied" partway through. Your account can read some objects but not all. Fix privileges, or export per-schema with an account that has full read access. A dump that errors out still leaves a partial file, so treat any error as a failed backup.
File exists but is tiny. You almost certainly exported schema only. Re-run with data included. Check whether your tool's export dialog or your command line added a structure-only flag.
Dump succeeds, restore fails. Look for encoding mismatches, MySQL's --hex-blob omission on binary data, or a server version difference. A dump from a newer MySQL might use syntax the old server rejects; export with the target server's version in mind.
Disk or shell truncation on very large tables. Redirect output to a file and verify its size at the end. If the shell buffers or the filesystem can't hold the output, split the export by table or by schema.
Writes during the dump. Without a consistent-snapshot setting, the restored data can be a mix of moments. For InnoDB, --single-transaction handles this; for other storage engines, schedule the dump when writes are quiet or lock the tables.
Sizing, Scheduling, and a Repeatable Backup Habit
A one-time export is a start, not a backup strategy. The practical habit: script the dump with a timestamped filename, run it on a schedule, and verify each run.
Sizing. Plain SQL files compress well, often by a large factor, so pipe through gzip when you can. But keep at least one uncompressed copy if a quick manual restore matters, or accept the extra minute to decompress. Estimate the file afterwards against the database's data size—an order-of-magnitude match is fine; a fraction of it means something's missing.
Scheduling. On a Linux server, a cron entry that runs the appropriate mysqldump, pg_dump, or sqlite3 .dump command daily and writes to a dated file is standard. Keep rotation—the last 7 daily files, for example—so disk space doesn't vanish.
The verification habit that actually prevents disasters. A backup file that has never been restored is a hypothesis. Restore the most recent dump into a scratch database periodically (weekly is a reasonable cadence for important data) and run row counts. If that sounds burdensome, automate it with a script that loads the latest dump into a disposable container and reports counts. The point is to catch a broken dump before you need it.
Where to store the copies. Keep the dump off the same machine—a separate network location, an external drive for SQLite and small databases, or cloud storage. A backup that lives only on the server it protects doesn't cover the most common failure.
None of this requires anything fancy. A working command, a timestamped file, a test restore, and a copy somewhere else cover the vast majority of real-world cases. The discipline is in the last three steps, not the first.