How to execute SQL commands from a file in PostgreSQL

Written by Ian Carnaghan · 19 sec read >

In my case I had a .sql file with hundreds of insert statements and I wanted to run all of them, I was left with two options namely manually entering every command via copy and paste or executing the file. I went for the second option and it worked. I used Postgres 8.4 on the command line on a Ubuntu machine.


Follow these steps

  • Connect to the relevant database eg.
    sudo -u postgres psql MYDB
  • Then run

    \i means execute commands from file

  • All commands will be executed.

Last Updated On September 30, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

Notify of