I’m currently writing some installer script that fires SQL files against different database types depending on the system’s configuration (the webapplication supports multiple database server like MySQL, MSSQL and PostgreSQL).
One of those types is PostgreSQL. I’m not fluent with it and I would like to know if it’s possible to make a statement into a define/populate SQL file that makes an SQL query conditional to a specific PostgreSQL server version.
How to make an SQL statement conditionally in plain PGSQL so that it is only executed in version 9? The command is:
ALTER DATABASE dbname SET bytea_output='escape';
The version check is to compare the version with 9.
Postgres does have
version()function, however there is nomajor_vesion(). Assuming that output string always includes version number asnumber(s).number(s).number(s)you could write your own wrapper as:Example:
However real issue here is that AFAIK you can’t execute your commands conditionally in “pure” SQL and best what you can do is to write some stored function like this:
I think that you should rather use some scripting language and generate appropriate SQL with it.