Run a PostgreSQL .sql File Using Command Line Arguments

psql is capable of running SQL statements written into a .sql file. Either, you want to run it on a local or remote server you can use psql client.

TL;DR: psql -f filename

Requirement

Either for new product installation, migration, cloning environment, we may need to run sql scripts written in files.

In my professional background, set up hundreds of software solutions to customers, each product comes with it’s own database script files. Either, your target database is PostgreSQL or not, each database engine has it’s own way to execute those scripts.

Run a SQL file Using Command Line Arguments

psql is the main client for PostgreSQL database engine. psql can interact with local and remote databases.

Run SQL File on a Local PostgreSQL Server

psql -d database_name -f file_name

Run SQL File on a Remote PostgreSQL Server

psql -d database_name -f file_name -h hostname

-h parameter can be used to specify hostname to execute script.

If you want to learn more about psql, you can check documentation from this link. Reading documentation, at least, to learn reading documentation is the best technical skill you can learn.

Please leave a comment below if you have anything to ask, or anything to add. I would like to meet you.

A new post everyday, subscribe now and don’t miss it!

Subscribe to our newsletter for cool news

Hi! I’m an IT Specialist

I want to hear from you! I am Working with enterprises for 10+ years to improve their infrastructure and efficiency.

Get in touch with me.

Leave a Reply

Discover more from Empower. Innovate. Transform.

Subscribe now to keep reading and get access to the full archive.

Continue reading