1

I'm developing application that holds data in postgres. So i must prepare database before working with application, there must be created few tables. I'm creating this tables by running sql code but i think it's not convenient after i found this doc:

A useful extension to PostgreSQL typically includes multiple SQL objects; for example, a new data type will require new functions, new operators, and probably new index operator classes. It is helpful to collect all these objects into a single package to simplify database management

The main advantage of using an extension, rather than just running the SQL script to load a bunch of "loose" objects into your database, is that PostgreSQL will then understand that the objects of the extension go together

I believe that i must use this approach

What i don't understand is that how can i share my extension. I thought that it works like maven, you create your extension with custom types, functions, tables and than you can pack it, name it (eg my-ext-0.1), give a version and release into some kind of a repository. After that you can connect to a database, run sql 'create extension my-ext-0.1' and have everything done :)

I thought that 'create extension' command will download extension and install it without downloading this by hands. I use maven, ivy and i expected similar behaviour from postgresql.

Documentation says that you need to place your extension files under some directory and only than run 'create extension' under some database.

How do you create your extensions and share them between different servers?

4

2 回答 2

2

Postgres 扩展不是这样工作的。他们可以访问数据库内部,并且可以作为数据库操作系统用户运行任何代码。因此,安装它们通常仅限于超级用户,来自特定目录,并且只有其中一些在托管托管服务器上可用。

我认为您可以通过在添加到搜索路径的特殊模式中安装补充函数、类型和表来实现类似的效果。升级将非常简单:

drop schema mylib cascade; -- don't do this!!!
create schema mylib;
\i mylib.sql

但不幸的是,这也会从其他模式中删除所有依赖对象 - 使用自定义类型的列,使用自定义函数的触发器等。所以它不是你问题的解决方案。

于 2017-05-05T21:19:56.880 回答
2

我宁愿使用可用的扩展和“标准”语言在我的模式中创建我的函数、类型和所有内容。

Postgres 不会下载您的扩展程序(除非您创建的扩展程序会将此功能添加到 postgres)。但是您的扩展程序仍应以“通常”的方式创建。

要检查您的“扩展目录”,请运行:

t=# create extension "where should I put control file";
ERROR:  could not open extension control file "/usr/local/share/postgresql/extension/where should I put control file.control": No such file or directory

并重复评论,在扩展 SQL之前,请检查plpgsql 和现有命令。

当您感到无聊并确保现有的 postgres 功能太有限时,请安装 postgres-contrib 包并检查其他扩展作为最佳实践。当然,请查看https://pgxn.org/

于 2017-05-05T21:22:47.807 回答