Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    4

    Question Unanswered: psql transactions in a bash script

    Hi guys,

    I'm trying to build a script which executes several psql queries. I'm using something like:
    Code:
    $ psql -U user -d database -c "query"
    The problem is, I'd like all those psql commands to execute in the same transaction.
    Anyone knows how can I do it?

    I've been googling around, and found that it may be possible to turn off an autocommit variable using:
    psql -P autocommit='off'
    but it didn't work

    Anyone knows how could this be done?!
    Thanks in advance!

    Pedro

  2. #2
    Join Date
    Aug 2009
    Location
    Olympia, WA
    Posts
    337
    Save a single .sql file that has all of your commands.
    Code:
    $ psql -U user -d database < mySQLscript.sql
    
    or 
    
    $ psql -U user -d database -f mySQLscript.sql

  3. #3
    Join Date
    Oct 2009
    Posts
    4
    Think your right...
    I've tried to do it with several psql commands, but the transactions management is very complicated, so it's better to gather all the needed information along the script, save it to a file. And then, in the final execute all queries from file, just as you mentioned.

    Thanks a lot for the help

  4. #4
    Join Date
    Aug 2009
    Location
    Olympia, WA
    Posts
    337
    but the transactions management is very complicated
    Not complicated, impossible. Each time you fire off a psql command, you are establishing a new connection. You can't have a single transaction across multiple connections.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •