0

Basically I have a handful of .sql files that I regularly modify in structure that need to be run in a certain order:

CREATE.sql,
INSERT.sql,
PLPSGSQL.sql,
TEST.sql,
DROP.sql

Since every structural modification requires a full re-run of this sequence I would like to save myself the trouble of tapping throgh my command history and compile them into a makefile-esque structure; e.g.

$ Makepsql
$ psql
> \i CREATE.sql
> \i INSERT.sql
> \i PLPSGSQL.sql
> \i TEST.sql
> \i DROP.sql
> \q

Is there something like this?

5
  • Check out tools like Liquibase or Flyway.
    – user1822
    Commented Oct 26, 2015 at 18:21
  • Sadly I'm not working on my own environment, but on an university server.
    – nitowa
    Commented Oct 26, 2015 at 18:57
  • So what? Liquibase is just a command line tool
    – user1822
    Commented Oct 26, 2015 at 19:43
  • You can include multiple files from a main one - and then just run this file using psql -f main_file.sql Commented Oct 26, 2015 at 21:55
  • I don't have any admin rights on that server, so I may not install any tools.. That's what.
    – nitowa
    Commented Oct 27, 2015 at 16:31

1 Answer 1

1

Write a bash script and pass those files as arguments to psql.

#!/bin/bash

set -e #abort on error

psql -v ON_ERROR_STOP=1 -f CREATE.sql
#...insert your scripts here
#...
psql -v ON_ERROR_STOP=1 -f DROP.sql

If you want the Makefile functionality (e.g. check modified dates of files and dependencies), just write a Makefile, and use the psql commands!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.