Tag: postgresql

在PostgreSQL 9.3上安装PL / Ruby

我想使用PL / Ruby过程语言,但似乎git存储库维护得不是很好: https : //github.com/Absolight/postgresql-plruby/tree/fixup-9-3 当我开始创建新语言时: create function plruby_call_handler() returns language_handler as ‘/var/lib/postgresql/.rvm/rubies/ruby-2.1.3/lib/ruby/site_ruby/2.1.0/x86_64-linux/plruby’ language ‘c’; — Warning: the ‘c’ has to be lowercase, unlike the example on git. 我收到以下错误: ERROR: could not load library “/var/lib/postgresql[…]plruby”: undefined symbol: plruby_s_load 我在Ubuntu 14.10上使用Ruby 2.1.4和PostgreSQL 9.3。

如何通过ransack gem搜索数组?

我正在使用ransack gem在rails应用程序中进行搜索。 我需要在User表中搜索email_ids数组。 在洗劫时提到这个问题,我按照步骤将其添加到初始化文件夹ransack.rb Ransack.configure do |config| { contained_within_array: :contained_within, contained_within_or_equals_array: :contained_within_or_equals, contains_array: :contains, contains_or_equals_array: :contains_or_equals, overlap_array: :overlap }.each do |rp, ap| config.add_predicate rp, arel_predicate: ap, wants_array: true end end 在rails控制台中,如果我喜欢这样: a = User.search(email_contains_array: [‘priti@gmail.com’]) 它产生这样的sql: “SELECT \”users\”.* FROM \”users\” WHERE \”users\”.\”deleted_at\” IS NULL AND (\”users\”.\”email\” >> ‘—\n- priti@gmail.com\n’)” 并给出如下错误: User Load (1.8ms) SELECT “users”.* […]

为什么我无法使用rails 4.0.0.rc1(ruby 2.0)安装postgresql

第一次在论坛上,所以我会尝试尽可能具体,抱歉任何可能的拼写错误? 我相对较新的rails(不到一年),我想制作一个默认项目来使用它。 我一直在开发中使用sqlite3而在生产中使用pg(heroku),但现在我想把事情弄好(我遇到了sqlite3运行正常但pg没有的问题)并在生产和devlop中同时使用pg。 我花了2天时间搜索foruns并且所有的anwsers都是旧的或其他版本/其他操作系统。 我将尝试列出我为设置舞台所采取的步骤:在这里我的配置: -OS:Windows 7 64x -Editor:Aptana Studio -PostGresql -Rails 4.0.0.rc1 -Ruby 2.0 下载并安装postgresql-9.2.4-1-windows-x64 将默认密码设置为postgresql 在postgresql中创建一个具有所有特权的角色“my_user” 创建测试数据库“rails_test” 打开Aptana Studio并初始化一个新的默认项目,转到项目文件夹,然后运行“rails new。-d postgresql”来生成新项目。 安装pg gem“gem install pg”“gem list”正确显示pg gem 配置“database.yml”以使用正确的用户名和密码 development: adapter: postgresql encoding: unicode database: rails_test pool: 5 username: my_user password: 12345 在ruby中运行一些命令,如“gem update”,“bundle install” 然后,当我尝试生成模型并使用rake db:migrate时,我收到以下错误: Specified ‘postgresql’ for database adapter, but the […]

Hstore和Rails

我正在尝试使用最新版本的activerecord-postgres-hstore gem在Rails 3.2.9项目中使用Hstore,并且我在使用ActiveRecord提供的store_accessor获取Hstore中的值时遇到了一些麻烦。 我的模型看起来像这样: class Person < ActiveRecord::Base serialize :data, ActiveRecord::Coders::Hstore attr_accessible :data, :name store_accessor :data, :age, :gender end 当我进入rails控制台时,我得到的是: Loading development environment (Rails 3.2.9) 1.9.3-p327 :001 > p = Person.find(3) Person Load (9.8ms) SELECT “people”.* FROM “people” WHERE “people”.”id” = $1 LIMIT 1 [[“id”, 3]] => #”34″, “gender”=>”male”}, created_at: “2012-12-07 15:01:53”, updated_at: “2012-12-07 15:27:40”> […]

Rails 5 SQL注入

我已经在一段时间内阅读了关于各种SO线程,指南等的内容……但所有答案都是相互矛盾和矛盾的。 似乎有许多类似的方法,很多答案都说使用不同的方法。 sanitize sanitize_conditions sanitize_sql sanitize_sql_array sanitize_sql_for_assignment sanitize_sql_for_conditions sanitize_sql_hash sanitize_sql_hash_for_assignment sanitize_sql_hash_for_conditions sanitize_sql_like 我正在尝试编写一个“原始查询”适配器,它允许我运行原始的Postgres查询,但允许我插入来自危险用户输入的我自己的参数。 我不能在这几个实例中使用AR,因为我正在进行复杂的纬度/经度计算,聚合函数,复杂子查询等。 到目前为止,我尝试了两种方法: 方法1 对于这种方法,我不知道sanitize是否是上述的最佳选择,或者它是否适用于100%的情况……(我只使用Postgres) class RawQuery def exec(prepared, *params) prepared = query.dup params.flatten.each_with_index do |p, i| prepared.gsub!(“$#{i + 1}”, ActiveRecord::Base.sanitize(p)) end ActiveRecord::Base.connection.exec_query(prepared) end end 琐碎的用法示例(当然通常不会这么简单,或者我只会使用AR): RawQuery.new.exec(‘SELECT * FROM users WHERE name = $1’, params[:name]) 此外,似乎sanitize代表quote 。 但根据这篇SOpost说它只是用单引号包装东西是不安全的…所以我不知道。 方法2 我不确定这是否同样安全,但它似乎使用了一个实际的PG准备function(我认为它是100%安全的)。 唯一的问题是rails不会将其打印到控制台,也不包括SQL执行时间(这会破坏我的分析工具)。 class RawQuery […]

如何将Rails时区名称映射到PostgreSQL?

我需要使用类似的查询 SELECT * FROM items WHERE to_char(created_at AT TIME ZONE ‘RAILS_GIVEN_ZONE’, ‘DD/MM/YYYY’) ILIKE ‘%5/02%’ 其中RAILS_GIVEN_ZONE值应始终使用Rails 4应用程序的时区(可由用户更改),而不是PG的timezone选项。 但问题是Rails和PG的时区并不完全对应1对1。 使用偏移量(如Time.zone.now.formatted_offset中的+10:00 )还不够好,因为在这种情况下,PG将无法正确处理夏令时。 所以问题是什么是自动将Rails当前时区( Time.zone )映射到PostgreSQL的指定时区的最佳方法? 注意: create_at列是timestamptz (存储为UTC,显示在任何需要的区域中)

DB上的错误:迁移“未初始化的常量DeviseCreateUsers”

我正在尝试在Heroku上运行rake db:migrate命令,我遇到了这个问题。 uninitialized constant DeviseCreateUsers /app/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:230:in `block in constantize’ /app/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:229:in `each’ /app/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.11/lib/active_support/inflector/methods.rb:229:in `constantize’ /app/vendor/bundle/ruby/2.0.0/gems/activesupport-3.2.11/lib/active_support/core_ext/string/inflections.rb:54:in `constantize’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:538:in `load_migration’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:533:in `migration’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:528:in `migrate’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:720:in `block (2 levels) in migrate’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `call’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `block in ddl_transaction’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `ddl_transaction’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:719:in `block in migrate’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `each’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `migrate’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:570:in `up’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/migration.rb:551:in `migrate’ /app/vendor/bundle/ruby/2.0.0/gems/activerecord-3.2.11/lib/active_record/railties/databases.rake:179:in `block (2 levels) […]

Rails + PostgreSQL SSL解密失败

我在我的生产服务器上运行了一个应用程序,它使用pg gem与Postgres数据库进行通信。 Postgres在默认端口上运行,并且位于防火墙之后 – 因此除了localhost ,它无法访问。 我没有配置Postgres做任何与SSL相关的事情。 我正在通过SSL访问Rails应用程序,并且证书已经签署了另一个域,所以第一次点击它时会出现证书错误…但这是SSL相关的唯一奇怪的东西。 然而,我在Rails日志中间歇性地看到这一点(当浏览器发生时伴随着500错误): Started GET “/admin/pages” for at 2012-02-02 01:52:03 -0500 Processing by PagesController#index as HTML Completed 500 Internal Server Error in 4ms ActiveRecord::StatementInvalid (PGError: SSL error: decryption failed or bad record mac : SELECT “pages”.* FROM “pages” ): app/controllers/pages_controller.rb:36:in `index’ 我勒个去?

Rails:在同一个应用程序中同时使用mysql和postgres?

为什么你会问? 因为我已经在mysql上构建了应用程序,并且只需要为我的应用程序的GIS组件开始使用postgres。 最终我将完全迁移到postgres,但同时想知道这是否可行

无法使用Ruby连接到Heroku上的PostgreSQL数据库 – 无法翻译主机名

我正在使用Ruby(而不是Rails)并连接PostgreSQL数据库。 我一直试图在Heroku上设置,但是我在启动应用程序时遇到了问题。 在本地运行应用程序运行正常。 我的本地.env看起来像: postgres://DATABASE_URL=localhost 并且连接到数据库的Ruby连接如下所示: @@db = PGconn.open(:hostaddr => ENV[‘DATABASE_URL’], :dbname => ‘(dbname)’, :password => ‘(password)’) 当我推送到Heroku时,应用程序在该行崩溃,并将此错误写入日志: could not translate host name “postgres://(my heroku db address)” to address: Name or service not known (PG::Error) 那里的数据库地址与我的heroku:config中的DATABASE_URL相匹配。 我正在使用共享数据库。 我尝试使用:host => ENV[‘DATABASE_URL’] (而不是:hostaddr ),但结果相同。 我猜我有一些简单的东西,但是我没有任何好的想法。