我正在部署使用PostgreSQL和HSTORE的Rails应用程序。
要部署它,我正在使用rubber。
一切正常,但HSTORE未正确启用除外。当包含execute("CREATE EXTENSION hstore")
的迁移运行时,我会收到以下错误:
** [out :: production.---]
** [out :: production.---] -- execute("CREATE EXTENSION hstore")
** [out :: production.---]
** [out :: production.---] rake aborted!
** [out :: production.---] An error has occurred, this and all later migrations canceled:
** [out :: production.---]
** [out :: production.---] PG::Error: ERROR: permission denied to create extension "hstore"
** [out :: production.---] HINT: Must be superuser to create this extension.
创建postgres实例的脚本具有以下代码:
create_user_cmd = "CREATE USER #{env.db_user} WITH NOSUPERUSER CREATEDB NOCREATEROLE"
所以我认为问题可能与此处设置的NOSUPERUSER
属性有关。
有没有办法在保持大部分生成的文件不变的情况下使用橡胶启用hstore?
答案 0 :(得分:5)
问题是橡胶会将数据库用户创建为NOSUPERUSER
。我的解决方法是创建在capistrano的deploy:migrate
之前和之后运行的任务,这些任务会将用户更改为SUPERUSER
并返回。
以下是代码:
namespace :rubber do
namespace :project do
before "deploy:migrate", "rubber:project:add_pg_superuser_and_enable_hstore"
after "deploy:migrate", "rubber:project:remove_pg_superuser"
task :add_pg_superuser_and_enable_hstore,
:roles => [:postgresql_master, :postgresql_slave] do
alter_user_cmd = "ALTER USER #{rubber_env.db_user} SUPERUSER"
create_hstore_cmd = "CREATE EXTENSION IF NOT EXISTS hstore;"
rubber.sudo_script "add_superuser_create_hstore", <<-ENDSCRIPT
sudo -i -u postgres psql -c "#{alter_user_cmd}"
sudo -i -u postgres psql -c "#{create_hstore_cmd}"
ENDSCRIPT
end
task :remove_pg_superuser, :roles => [:postgresql_master,
:postgresql_slave] do
alter_user_cmd = "ALTER USER #{rubber_env.db_user} NOSUPERUSER"
rubber.sudo_script "add_superuser_create_hstore", <<-ENDSCRIPT
sudo -i -u postgres psql -c "#{alter_user_cmd}"
ENDSCRIPT
end
end
end
答案 1 :(得分:0)