Execute several .sql files in a single transaction using PostgreSQL and bash

后端 未结 4 1357
闹比i
闹比i 2021-01-02 08:50

Say I have the files:

file1.sql
file2.sql
file3.sql

I need all three files to be executed in a single transaction. I\'m looking for a bash

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 09:36

    Either use a sub-shell:

    #!/bin/sh
     (echo "BEGIN;"; cat file1.sql; cat file2.sql; echo "COMMIT;") \
     | psql -U the_user the_database
    
    #eof
    

    or use a here-document:

    #!/bin/sh
    psql -U the_user the_database <

    NOTE: in HERE-documents there will be no globbing, so file*sql will not be expanded. Shell-variables will be expanded, even within quotes.

提交回复
热议问题