标签: rails-postgresql

在尝试安装pg gem时找不到'libpq-fe.h标头

我正在使用Ruby on Rails 3.1预发行版.我喜欢使用PostgreSQL,但问题是安装pggem.它给了我以下错误:

$ gem install pg
Building native extensions.  This could take a while...
ERROR:  Error installing pg:
    ERROR: Failed to build gem native extension.

        /home/u/.rvm/rubies/ruby-1.9.2-p0/bin/ruby extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for …
Run Code Online (Sandbox Code Playgroud)

postgresql ruby-on-rails pg ruby-on-rails-3 rails-postgresql

753
推荐指数
25
解决办法
27万
查看次数

PG :: ConnectionBad - 无法连接到服务器:连接被拒绝

每次我运行我的rails 4.0服务器,我得到这个输出.

Started GET "/" for 127.0.0.1 at 2013-11-06 23:56:36 -0500

PG::ConnectionBad - could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
: …
Run Code Online (Sandbox Code Playgroud)

ruby database-connection ruby-on-rails pg rails-postgresql

253
推荐指数
15
解决办法
22万
查看次数

恢复PostgreSQL数据库时如何解决权限问题

我使用该命令为Postgres数据库转储了一个干净的,没有所有者备份

pg_dump sample_database -O -c -U

后来,当我用数据库恢复时

psql -d sample_database -U app_name

但是,我遇到了一些错误,导致我无法恢复数据:

ERROR:  must be owner of extension plpgsql
ERROR:  must be owner of schema public
ERROR:  schema "public" already exists
ERROR:  must be owner of schema public
CREATE EXTENSION
ERROR:  must be owner of extension plpgsql
Run Code Online (Sandbox Code Playgroud)

我深入研究了纯文本SQL pg_dump生成,发现它包含SQL

CREATE SCHEMA public;
COMMENT ON SCHEMA public IS 'standard public schema';
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
Run Code Online (Sandbox Code Playgroud)

我认为原因是用户 …

postgresql database-backups rails-postgresql

89
推荐指数
7
解决办法
9万
查看次数

Postgresql适配器(pg):无法连接到服务器

每次运行我的Rails应用程序时都会收到此错误(它无法连接到我的本地Postgresql)

/Users/leonardo/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/
active_record/connection_adapters/postgresql_adapter.rb:1208:in `initialize': 
could not connect to server: No such file or directory (PG::Error)
   Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
Run Code Online (Sandbox Code Playgroud)

我正在使用Postgres.app,它正确运行.

如果我跑

$ psql
Run Code Online (Sandbox Code Playgroud)

我可以正确登录到Postgresql控制台.

$ which psql
 /Applications/Postgres.app/Contents/MacOS/bin/psql
Run Code Online (Sandbox Code Playgroud)

的Gemfile

source 'https://rubygems.org'
ruby "1.9.3"

gem 'rails', '3.2.11'
gem "pg"
Run Code Online (Sandbox Code Playgroud)

database.yml的

development:
  adapter: postgresql
  encoding: unicode
  username: leonardo
  password: 
  database: zapping
  port: 5432  
Run Code Online (Sandbox Code Playgroud)

Postgresql(控制台)

$ psql
leonardo=# \l
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

postgresql ruby-on-rails pg rails-postgresql

65
推荐指数
9
解决办法
7万
查看次数

Postgres JSON数据类型Rails查询

我正在使用Postgres的json数据类型,但想要使用嵌套在json中的数据进行查询/排序.

我想在json数据类型上使用.where进行排序或查询.例如,我想查询跟随者数> 500的用户,或者我想通过关注者或跟随计数来订购.

谢谢!

例:

model User

data: {
     "photos"=>[
       {"type"=>"facebook", "type_id"=>"facebook", "type_name"=>"Facebook", "url"=>"facebook.com"}
      ], 
     "social_profiles"=>[
         {"type"=>"vimeo", "type_id"=>"vimeo", "type_name"=>"Vimeo", "url"=>"http://vimeo.com/", "username"=>"v", "id"=>"1"},
         {"bio"=>"I am not a person, but a series of plants", "followers"=>1500, "following"=>240, "type"=>"twitter", "type_id"=>"twitter", "type_name"=>"Twitter", "url"=>"http://www.twitter.com/", "username"=>"123", "id"=>"123"}
     ]
}
Run Code Online (Sandbox Code Playgroud)

postgresql json rails-postgresql psql ruby-on-rails-4

51
推荐指数
2
解决办法
3万
查看次数

PostgreSQL date()带有时区

我在从Postgres正确选择日期时遇到问题 - 它们以UTC格式存储,但未正确转换为Date()函数.

将时间戳转换为日期会给我错误的日期,如果它超过太平洋标准时间下午4点.

2012-06-21应该是2012-06-20这种情况.

starts_at列数据类型是timestamp without time zone.这是我的疑问:

无需转换为PST时区:

Select starts_at from schedules where id = 40;

      starts_at      
---------------------
 2012-06-21 01:00:00
Run Code Online (Sandbox Code Playgroud)

转换给出了这个:

Select (starts_at at time zone 'pst') from schedules where id = 40;
        timezone        
------------------------
 2012-06-21 02:00:00-07
Run Code Online (Sandbox Code Playgroud)

但是都没有转换为时区中的正确日期.

postgresql timezone datetime rails-postgresql postgresql-9.1

43
推荐指数
3
解决办法
7万
查看次数

批量插入导轨3

我想从我的Rails应用程序中将几千条记录批量插入数据库(在我的情况下为POSTGRES).

这样做的"Rails方式"是什么?一些快速且正确的方法.

我知道我可以通过字符串连接属性来创建SQL查询,但我想要一个更好的方法.

ruby activerecord ruby-on-rails ruby-on-rails-3 rails-postgresql

38
推荐指数
2
解决办法
3万
查看次数

在PostgreSQL上创建非强制扩展时出错

我正在尝试将PostgreSQL配置为在我的rails应用程序中使用全文搜索,如此Railscast中所述.

我正在使用与with一起Ubuntu 12.04运行PostgreSQL 9.1.5安装的新服务器.apt-getppa:pitti/postgresqlprecise

尝试运行迁移时,当我在psql控制台中使用peer postgres用户尝试相同的命令时,我收到以下错误:

postgres=# CREATE EXTENSION unaccent;
Run Code Online (Sandbox Code Playgroud)

错误:无法打开扩展控制文件"/usr/share/postgresql/9.1/extension/unaccent.control":
没有这样的文件或目录

在我的本地框运行中,Ubuntu 10.04 desktop我使用相同的存储库(natty),它运行良好.

任何见解将不胜感激.

postgresql full-text-search unaccent rails-postgresql postgresql-9.1

35
推荐指数
2
解决办法
2万
查看次数

Ruby中DateTime的毫秒分辨率

我有一个字符串2012-01-01T01:02:03.456,我使用ActiveRecord存储在Postgres数据库TIMESTAMP中.

不幸的是,Ruby似乎切断了毫秒:

ruby-1.9.3-rc1 :078 > '2012-12-31T01:01:01.232323+3'.to_datetime
 => Mon, 31 Dec 2012 01:01:01 +0300 
Run Code Online (Sandbox Code Playgroud)

Postgrs支持微秒级分辨率.如何才能相应地保存我的时间戳?我需要至少毫秒的分辨率.

(PS是的,我可以在postgres中使用毫秒整数列进行破解;这种方法会破坏ActiveRecord的整个目的.)

更新:
非常有用的响应表明,Ruby DateTime没有削减毫秒; 使用#to_f显示它.但是,做:

m.happened_at = '2012-01-01T00:00:00.32323'.to_datetime
m.save!
m.reload
m.happened_at.to_f
Run Code Online (Sandbox Code Playgroud)

是否丢弃毫秒.

现在,有趣的是,created_at在Rails和Postgres中都显示毫秒.但是其他时间戳字段(happened_at如上所述)则没有.(也许Rails使用NOW()函数而created_at不是传入DateTime).

这导致了我的终极问题:
如何让ActiveRecord在时间戳字段上保留毫秒分辨率?

ruby postgresql activerecord datetime rails-postgresql

26
推荐指数
2
解决办法
2万
查看次数

如何为本地Rails项目设置Postgres数据库?

我最近买了一台新机器,现在想从Github处理我的项目.我很好奇如何在我的本地机器上正确设置Postgres数据库.我有postgresql,pgadmin3libpq-dev安装在Ubuntu(12.04)上.

我拉下项目:

git clone https://github.com/thebenedict/cowsnhills.git

并运行:

bundle.

当我跑:

rake db:create && rake db:schema:load

我收到此错误:

rake db:create && rake db:schema:load
FATAL:  password authentication failed for user "cnh"
FATAL:  password authentication failed for user "cnh"
....
Run Code Online (Sandbox Code Playgroud)

config/database.yml文件如下所示:

development:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: cnh_development
  pool: 5
  username: cnh
  password: cnh

test:
  adapter: postgresql
  encoding: unicode
  host: localhost
  database: cnh_test
  pool: 5
  username: cnh
  password: cnh

production:
  adapter: postgresql
  encoding: unicode
  host: …
Run Code Online (Sandbox Code Playgroud)

postgresql ubuntu ruby-on-rails rails-postgresql postgresql-9.1

26
推荐指数
2
解决办法
4万
查看次数