.sql

What is a .sql file?

SQL Script · Code and data · IBM (SQL language) · 1970s

A SQL file is a plain-text script of database commands, such as creating tables or inserting and querying data. Opening it is harmless, but running it against a database executes those commands, which can change or delete data.

Category
Code and data
Type
Plain text
Is a .sql file safe to open? Safe to open, it is plain text. The caution is running it: executing a .sql runs its commands against a database and can drop tables or delete data. Read it first and only run trusted scripts against a database you can restore.

What is a .sql file used for?

A .sql file holds SQL statements: the language databases speak, with commands like SELECT, INSERT, CREATE and DROP. It is often a database dump (a backup as text), a schema setup, or a migration.

Reading a .sql is completely safe, it is just text. The care is in running it: executing the script against a database carries out every command in it, which can include deleting rows or dropping whole tables.

You run one in a database client such as psql, the MySQL command line, SQLite, or a GUI like DBeaver, pointed at the database you want to change.

Open it in a text editor first to see exactly what it will do, especially any DROP or DELETE statements.

How to open a .sql file

The .sql format opens in the following apps, grouped by operating system. If one app does not work, try another from the same list.

Windows

  • VS Code or Notepad++ (to read/edit)
  • DBeaver / a DB client (to run)

macOS

  • VS Code (to read/edit)
  • DBeaver / a DB client (to run)

Linux

  • Any text editor (to read/edit)
  • psql / mysql / sqlite (to run)

Web

  • Online SQL editors (against a test database)

How to convert a .sql file

You can convert a .sql file to Not convertible using a conversion tool or the export menu of an app that opens it.

.sql file signature (magic bytes)

Programs recognise a .sql file by the bytes at the start of the file, not by its name. These are the signatures for SQL.

Hex signatureOffsetNote
(no fixed signature)0plain text; no fixed signature

See the full magic-numbers reference for every format.

Frequently asked questions

What is a .sql file?

A SQL file is a plain-text script of database commands, such as creating tables or inserting and querying data. Opening it is harmless, but running it against a database executes those commands, which can change or delete data.

How do I open a SQL file?

Open it in any text editor to read the commands. To actually run it, load it into a database client such as DBeaver, psql or the MySQL command line and execute it against your database.

How do I run a .sql file?

In a database client, connect to your database and run the script, for example psql -f file.sql, mysql < file.sql, or the Run button in a GUI. Read it first, since it will carry out every command it contains.

Related formats

Sources

Details on this page were checked against authoritative references: