我在 postgresql 中有两个用户,其中一个名为 migration
,当 Rails 在生产服务器上运行迁移时使用。此用户拥有生产数据库。我还有 production
用户,他应该只有以下权限:SELECT、INSERT、UPDATE、DELETE
在生产数据库上。
问题是,每次创建新表时,我都必须在 psql 中手动运行它:
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO production;
-- next line is needed for each new table which has an auto incrementing field, in this case - table `users`
GRANT USAGE, SELECT ON SEQUENCE users_id_seq TO production;
因为 production
用户对新建表的权限不会自动设置。运行迁移时自动执行此操作的最佳方法是什么?有任何可用于 Rails/Capistrano 的脚本吗?
最佳答案
您可以使用 Postgres 的 ALTER DEFAULT PRIVILEGES
来获得它 automatically assign the rights为所有新创建的表生产。
或者,您可以编写自定义 Capistrano 任务来设置通过 after "deploy:migrate", "mycustomtaskname"
Hook 调用的权限。 This pastie可能会给你一些关于如何通过 Capistrano 与 pgsql 交互的好提示,例如如何以交互方式提供密码。
关于ruby-on-rails - Rails 迁移和 PostgreSQL : separate user for migration and setting privileges for production user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10716269/