如果扩展名不可用,则“CREATE EXTENSION”不会失败

时间:2013-06-15 01:04:48

标签: sql postgresql compatibility

在迁移脚本中,我有以下内容:

CREATE EXTENSION json;

问题是,在PostgreSQL的某些版本中,似乎JSON是内置的,而在其他版本中,它可以作为扩展。

如果我尝试创建的扩展程序不可用,如何使CREATE EXTENSION ...失败?

1 个答案:

答案 0 :(得分:2)

一些可能很方便的功能:

http://www.postgresql.org/docs/current/static/view-pg-available-extensions.html

除了在发出语句之前进行检查之外,您还可以将代码包装在do语句中并捕获错误:

do $$
begin
  ; -- do something
exception
when ...
  ; -- do something different
end;
$$ language plpgsql;

http://www.postgresql.org/docs/current/static/sql-do.html

http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html

http://www.postgresql.org/docs/current/static/errcodes-appendix.html