通过没有超级用户角色的迁移在 PostgreSQL 数据库中安装扩展?

Install extension in PostgreSQL database via migration without superuser role?

我们正在使用迁移(通过 Sequelize,在 JavaScript 中)来维护对我们数据库的更改。我需要添加一个 CREATE EXTENSION 调用,但由于我是 运行 作为数据库创建者,而不是超级用户,我得到一个 permission denied to create extension.

有没有办法修改单个数据库的安全性以允许用户通过迁移文件安装扩展? IOW,当我创建 "naked" 数据库并应用我的权限时,我可以设置安全性以允许特定用户使用 CREATE EXTENSIONDROP EXTENSION 吗?

不,CREATE ROLE for granular CREATE/DROP EXTENSION control. I assume, it depends on the extension you use, but you based on the documentation没有设置:

Loading an extension requires the same privileges that would be required to create its component objects. For most extensions this means superuser or database owner privileges are needed. The user who runs CREATE EXTENSION becomes the owner of the extension for purposes of later privilege checks, as well as the owner of any objects created by the extension's script.

我 运行 遇到了与 pg_trgm 扩展名相同的问题,但即使将角色设置为数据库所有者也没有解决问题。

无法在迁移文件中找到执行此操作的方法。

我们最终使用的技术是在迁移文件夹中有一个名为 postgres 的子文件夹,用于保存必须 运行 作为超级用户的任何内容。该子文件夹包含迁移和一个配置文件,用于 运行 以超级用户身份进行操作。因此,命令行变为:

$ sequelize db:migrate --migrations-path "migrations/postgres" --config "migrations/postgres"
$ sequelize db:migrate

需要这种隔离的 "special" 迁移也使它们更加明显,从而减轻了更复杂的命令行带来的不便。

对于仍在寻找的人,这里是如何通过 sequelize 迁移创建扩展。但是,它没有解决用户角色的次要问题。但这对我有用。

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.query('CREATE EXTENSION extensionName;');
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.sequelize.query('DROP EXTENSION extensionName;');
  }
};