How to open .SQL files on Linux

To open .SQL files on Linux, to view/edit: open the file in a text editor (for example, a terminal editor) since .sql is plain text.

Step-by-step instructions

  1. To view/edit: open the file in a text editor (for example, a terminal editor) since .sql is plain text.
  2. To execute in PostgreSQL: run psql from a terminal and execute the script file (see the PostgreSQL psql documentation).
  3. On Linux desktops, file type association can use the shared MIME-info database; the file is typically mapped by extension to a MIME type such as application/sql per freedesktop.org shared-mime-info.

Common issues

Double-clicking opens the file in the wrong program

.sql is plain text, so the OS may associate it with an unexpected editor or a database tool you don’t want to use for viewing.

  1. Use “Open with” to choose your preferred text editor for reading/editing.
  2. If you need to run it, open your database client (for example psql) and execute the script from there instead of double-clicking.

Script runs but fails with syntax errors

SQL syntax and supported features can vary between database systems, so a script generated for one product may not run unchanged on another.

  1. Confirm which database the script targets (for example PostgreSQL vs MySQL) and run it using the matching client/tool (psql for PostgreSQL; MySQL CLI/Workbench for MySQL).
  2. If you must run it on a different database, edit the SQL to match that database’s dialect and features, then try again.

Import/execute fails due to missing database connection or permissions

Executing a .sql file requires connecting to the right server/database and having privileges to create/alter objects or modify data.

  1. Verify you are connecting to the intended database instance and database name in your client (for example in psql connection options).
  2. Ask for or grant the required permissions before re-running the script.

Security note

.sql files can contain destructive statements (for example dropping tables or deleting data). Read the script in a text editor before executing it, especially if it came from an untrusted source.

Back to .SQL extension page