Ruby-on-rails 错误:必须是语言 plpgsql 的所有者
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4368789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR: must be owner of language plpgsql
提问by Lee Hambley
I'm using PostgreSQL v9.0.1with Rails(and it's deps) @ v2.3.8, owing to the use of the fulltext capability of postgres, I have a table which is defined as:
我正在使用PostgreSQL v9.0.1with Rails(and it's deps) @ v2.3.8,由于使用了 postgres 的全文功能,我有一个表定义为:
CREATE TABLE affiliate_products (
id integer NOT NULL,
name character varying(255),
model character varying(255),
description text,
price numeric(9,2),
created_at timestamp without time zone,
updated_at timestamp without time zone,
textsearch_vector tsvector,
);
Note the last line, this ensures that active record isn't able to process it with the standard schema dumper, so I have to set config.active_record.schema_format = :sqlin ./config/environment.rb; and use rake db:test:clone_structureinstead of rake db:test:clone.
请注意最后一行,这确保活动记录无法使用标准模式转储程序处理它,因此我必须config.active_record.schema_format = :sql在./config/environment.rb; 并使用rake db:test:clone_structure代替rake db:test:clone.
None of this is too remarkable, only inconvenient - however rake db:test:clone_structurefails with the error:
这一切都不是太引人注目,只是不方便 - 但是rake db:test:clone_structure失败并出现错误:
ERROR: must be owner of language plpgsql
ERROR: must be owner of language plpgsql
Because of line #16in my resulting ./db/development_schema.sql:
由于#16我的结果中的行./db/development_schema.sql:
CREATE OR REPLACE PROCEDURAL LANGUAGE plpgsql;
CREATE OR REPLACE PROCEDURAL LANGUAGE plpgsql;
Under PostgreSQL v9.0+the language plpsqlis installed by the superuser, to the initial template, which is then available to the newly created schema.
在PostgreSQL v9.0+语言plpsql是由超级用户安装,初始模板,然后将其提供给新创建的模式。
I cannot run tests on this project without resolving this, and even editing ./db/development_schema.sqlmanually is futile as it is regenerated every time I run rake db:test:clone_structure(and ignored by rake db:test:clone).
如果不解决这个问题,我就无法在这个项目上运行测试,即使./db/development_schema.sql手动编辑也是徒劳的,因为它每次运行时都会重新生成rake db:test:clone_structure(并被 忽略rake db:test:clone)。
I hope someone can shed some light on this?
我希望有人能对此有所了解?
Note:I have used both the pg 0.9.0adapter gem, and the postgresgem at version 0.7.9.2008.01.28- both display identical behaviour.
注意:我使用了pg 0.9.0适配器 gem 和postgres版本中的gem 0.7.9.2008.01.28- 两者都显示相同的行为。
My teammates run PostgreSQL v8.4where the language installation is a manual step.
我的队友PostgreSQL v8.4在语言安装是手动步骤的地方运行。
采纳答案by Lee Hambley
The solution was as follows:
解决方法如下:
On my installation, there are standard templates template0and template1- at least as I understand it postgres will look for the highest numbered templateNwhen creating a new database, unless the template is specified.
在我的安装,有标准模板template0和template1-至少我了解Postgres将寻找最高编号templateN创建一个新的数据库时,除非指定的模板。
In this instance, as template0included plpgsql, so did template1… the idea being that you will customise template1to suite your site specific default needs, and in the case that you blow everything up, you would restore template1from template0.
在这种情况下,template0包括在内plpgsql,也是如此template1......想法是您将自定义template1以满足您站点特定的默认需求,并且在您炸毁所有内容的情况下,您template1将从template0.
As my site specific requirement was to install plpgsqlas part of the automated build of my web application (a step we had to keep to maintain 8.4 compatibility) - the solution was easy: remove plpgsqlfrom template1and the warning/error went away.
由于我的网站的具体要求是安装plpgsql我的web应用程序(一个步骤中,我们必须继续保持8.4兼容性)的自动构建的一部分-该解决方案很简单:删除plpgsql从template1和警告/错误就走开了。
In the case that the site-specific defaults would change, and we should need to go back to the default behaviour, we would simply remove template1and recreate it (which would use template0)
如果特定于站点的默认值会更改,并且我们需要返回默认行为,我们只需删除template1并重新创建它(这将使用template0)
回答by David Dehghan
I had the same problem. I fixed my template with the commands below
我有同样的问题。我用下面的命令修复了我的模板
psql template1
template1=# alter role my_user_name with superuser;
read more at http://gilesbowkett.blogspot.com/2011/07/error-must-be-owner-of-language-plpgsql.html
在http://gilesbowkett.blogspot.com/2011/07/error-must-be-owner-of-language-plpgsql.html阅读更多信息
回答by Frank
For new readers, I read this older post after having run into this error in one of my own projects. I strongly feel that giving the app's PostgreSQL a superuser role is a terrible idea and changing the template is not ideal either. Since the referenced PSQL commands that are added by db:structure:dumpare not needed by the Rails app's database, I have written a custom rake task that comments out the problematic lines in structure.sql. I have shared that code publicly on Github as a Gist at https://gist.github.com/rietta/7898366.
对于新读者,在我自己的一个项目中遇到此错误后,我阅读了这篇较旧的文章。我强烈认为给应用程序的 PostgreSQL 一个超级用户角色是一个糟糕的主意,改变模板也不理想。由于db:structure:dumpRails 应用程序的数据库不需要添加的引用 PSQL 命令,因此我编写了一个自定义 rake 任务,注释掉 structure.sql 中的有问题的行。我在https://gist.github.com/rietta/7898366上作为 Gist 在 Github 上公开共享了该代码。
回答by Phil Loden
I encountered this error while attempting to do RAILS_ENV=development bundle exec rake db:reset. I was able to accomplish the same thing (for my purposes) by doing RAILS_ENV=development bundle exec rake db:drop db:create db:migrateinstead.
我在尝试执行时遇到此错误RAILS_ENV=development bundle exec rake db:reset。我能够通过这样做来完成同样的事情(为了我的目的)RAILS_ENV=development bundle exec rake db:drop db:create db:migrate。
回答by bonkydog
I just filter the plpgsql extension statements from the structure.sql file post-dump:
我只是从转储后的 structure.sql 文件中过滤 plpgsql 扩展语句:
# lib/tasks/db.rake
namespace :db do
desc "Fix 'ERROR: must be owner of extension plpgsql' complaints from Postgresql"
task :fix_psql_dump do |task|
filename = ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
sql = File.read(filename)
sql.sub!(/(CREATE EXTENSION IF NOT EXISTS plpgsql)/, '-- ')
sql.sub!(/(COMMENT ON EXTENSION plpgsql)/, '-- ')
File.open(filename, 'w') do |f|
f.write(sql)
end
task.reenable
end
end
Rake::Task["db:structure:dump"].enhance do
Rake::Task["db:fix_psql_dump"].invoke
end

