Terminus by Warp
How To Run a SQL File With psql

How To Run a SQL File With psql

Razvan Ludosanu
Razvan Ludosanu
Founder, learnbackend.dev

[#the-short-answer]The Short Answer[#the-short-answer]

 $ psql -U user_name -d database_name < file.sql

[.inline-code]psql[.inline-code] is a command-line tool that enables you to connect to a PostgreSQL instance and interactively administer it by running SQL commands. These commands are used to manage users, roles, databases, and databases objects such as tables, views, indexes, etc., by performing CRUD (i.e. Create, Read, Update, Delete) operations against them.

While most database operations can be performed manually, it is sometimes necessary to automate them using SQL files. For example, when replicating an existing database structure and its objects, or migrating thousands of database entries into another PostgreSQL instance.

In this post, we’ll discuss how to import and execute a SQL file into a PostgreSQL database using the [.inline-code]psql[.inline-code] command, and how to resolve the most common errors that might occur when doing so.

[#importing-SQL-file-using-psql]Importing a SQL file using the [.inline-code]psql[.inline-code] command[#importing-SQL-file-using-psql]

The first method for importing a SQL file in PostgreSQL is to use the input redirection operator [.inline-code]<[.inline-code] which causes a program to read from a file instead of the standard input. When used, the [.inline-code]psql[.inline-code] command will behave as if the commands contained in the SQL file were manually entered by a user through the command-line interface.

 $ psql -U user_name -d database_name < file.sql

The second method for importing a SQL file is to use the [.inline-code]-f[.inline-code] option flag, which will cause the [.inline-code]psql[.inline-code] command to behave the same way as with the previous method, while enabling additional features such as error messages with line numbers.

 $ psql -U user_name -d database_name -f file.sql

In both cases:

  • The [.inline-code]-U[.inline-code] option flag is used to specify the name of the user under which the SQL commands will be executed.
  • The [.inline-code]-d[.inline-code] option flag is used to specify the name of the database these commands will be executed on.

[#fixing-common-errors]Fixing common errors[#fixing-common-errors]

Importing a SQL file using the [.inline-code]psql[.inline-code] command might fail for various reasons. Here is a list of the most common errors you might encounter, and that you will need to resolve before the file can successfully be imported.

[#invalid-password][.inline-code]invalid_password[.inline-code]: Invalid password[#invalid-password]

The [.inline-code]invalid_password[.inline-code] error indicates that you entered the wrong password for the specified user.

If the error persists after several attempts, you can connect to the PostgreSQL instance using an account with elevated privileges, and verify that the specified user exists using the [.inline-code]du[.inline-code] command:

 psql> \du

Or change the user’s password using the [.inline-code]ALTER USER[.inline-code] command:

 psql> ALTER USER user_name WITH PASSWORD ‘new_password’;

[#insufficient-privileges][.inline-code]insufficient_privilege[.inline-code]: Insufficient privileges[#insufficient-privileges]

The [.inline-code]insufficient_privilege[.inline-code] error indicates that the specified user doesn’t have the necessary privileges to perform the actions contained in the SQL file.

You can solve this by connecting into the PostgreSQL instance using an account with elevated privileges, and granting a different set of privileges to the specified user using the [.inline-code]GRANT[.inline-code] command.

 psql> GRANT ALL ON  table_name TO user_name;

[#file-not-found][.inline-code]undefined_file[.inline-code]: File not found[#file-not-found]

The [.inline-code]undefined_file[.inline-code] error indicates that the file you are trying to import doesn’t exist.

You can solve this by making sure that the file actually exists, and that the provided file path is valid.

[#permission-denied]Permission denied[#permission-denied]

This error indicates that the provided PostgreSQL user doesn’t have permission to read the file you are trying to import.

You can verify the file’s permissions using the [.inline-code]ls -l[.inline-code] command, and update them using the [.inline-code]chmod[.inline-code] command.

For example:

 $ chmod a+r file.sql

[#syntax-error][.inline-code]syntax_error[.inline-code]: Syntax error[#syntax-error]

The [.inline-code]syntax_error[.inline-code] error indicates that the SQL file contains one or more syntax errors.

You can solve this by either referring to the official PostgreSQL documentation or by testing your file using an online syntax checker.

[#table-already-exists][.inline-code]duplicate_table[.inline-code]: Table already exists[#table-already-exists]

The [.inline-code]duplicate_table[.inline-code] error indicates that you are trying to create a database table that already exists.

You can solve this by either first dropping (i.e. permanently deleting) the existing table using the [.inline-code]DROP TABLE[.inline-code] command before re-running your SQL file, or using the [.inline-code]CREATE TABLE IF NOT EXISTS[.inline-code] command in your SQL file to skip this command if the table already exists.

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own
brew install --cask warp
Copied!
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.
Join the Linux waitlist or join the Windows waitlist
Join the Windows waitlist:
Success! You will receive an email from Warp when the release is available to download.
Oops! Something went wrong while submitting the form.