Home Databases pgSql How to execute SQL commands from a file in postgresql?
 
 
 

How to execute SQL commands from a file in postgresql?

PDF Print E-mail

Question: How to execute SQL commands from a file in postgresql?

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.

Answer:

Please follow the following steps

  • Connect to the relevant database eg.
    sudo -u postgres psql MYDB
  • Then run
    \i MYFILEWITHCOMMANDS.SQL
    \i means execute commands from file
  • All commands will be executed.  
+/-
+/- Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
Security Please input the anti-spam code that you can read in the image.
 

Go Daddy $7.49 .com Sale!