Skip to content
A git mirror of the 'pg' gem.
C Ruby C++
Latest commit e5cb1df Dec 25, 2015 @larskanis larskanis Respect character encoding of all strings sent to the server.
Previously all strings sent to the server were sent in their internal
binary representation, without respecting the character encoding
of strings. Now the encoding of all strings is compared with the
current connection encoding and converted if they are different.

Since coders are independent from any database connection, this
adds a second parameter to PG::Coder#encode, that allows to define
the destination encoding, which previously was always ASCII_8BIT.
This encoding should be set to the connection encoding, in practice.

This also adds a lot of tests for encoding and decoding data.

This implements issue #231 : https://bitbucket.org/ged/ruby-pg/issues/231
Failed to load latest commit information.
certs Add gem cert, trying out gemspec-based setup. Jan 31, 2016
ext Respect character encoding of all strings sent to the server. Dec 25, 2015
lib Fix the example for PG::BasicTypeMapForQueries. Jan 31, 2016
misc Update windows cross compilation tasks to the latest Openssl+Postgres… Jun 17, 2012
sample Whitespace cleanup Mar 26, 2013
spec Respect character encoding of all strings sent to the server. Dec 25, 2015
.gemtest Converted to Hoe Oct 7, 2011
.hgignore Ignore some build artifacts Nov 13, 2015
.hgsigs Added signature for changeset 75d4f016a17f Nov 13, 2015
.hgtags Added tag v0.18.4 for changeset f61127650cd0 Nov 13, 2015
.hoerc Add gem cert, trying out gemspec-based setup. Jan 31, 2016
.irbrc Updated build system Jan 3, 2010
.pryrc Rearranging classes under the PG namespace to be a better Ruby citizen Jan 24, 2012
.rvm.gems Bump the rvm gemset gem versions May 14, 2015
.tm_properties Fix indent in project settings Jun 7, 2013
.travis.yml Bundler is not installed per default on ruby-2.3.0 on travis-ci. Jan 9, 2016
BSDL Add missing BSDL license file (fixes #108) Feb 9, 2012
Contributors.rdoc Update Rafał Bigaj's email address in Contributors.rdoc at his request Oct 1, 2012
Gemfile Add gem cert, trying out gemspec-based setup. Jan 31, 2016
History.rdoc Bump the version, add date to History Nov 13, 2015
LICENSE Updated/simplified authors/license sections of the README, API doc cl… Jan 24, 2012
Manifest.txt Keep the manifest sorted so it can be diffed. Jan 3, 2015
POSTGRES fix cross compilation of win32 binary gem, fix mixed case file naming Jan 27, 2012
README-OS_X.rdoc More case-corrections Jan 27, 2012
README-Windows.rdoc Update README-Windows in regards to rake-compiler-dock. Jun 11, 2015
README.ja.rdoc Update READMEs Feb 27, 2013
README.rdoc fix indent sample code Jan 30, 2016
Rakefile Remove GPL from the `licenses` of the Hoespec/gemspec (fixes #72) Mar 22, 2016
Rakefile.cross rake-compiler-dock-0.5.1 fixes the issue with bundler while cross com… Jan 30, 2016
appveyor.yml Update appveyor and travis-ci tests from 9.4 and 9.0 to 9.5 and 9.1. Jan 9, 2016
pg.gemspec Remove GPL from the `licenses` of the Hoespec/gemspec (fixes #72) Mar 22, 2016

README.rdoc

pg

home

bitbucket.org/ged/ruby-pg

mirror

github.com/ged/ruby-pg

docs

deveiate.org/code/pg

Join the chat at
https://gitter.im/ged/ruby-pg

Description

Pg is the Ruby interface to the PostgreSQL RDBMS.

It works with PostgreSQL 8.4 and later.

A small example usage:

#!/usr/bin/env ruby

require 'pg'

# Output a table of current connections to the DB
conn = PG.connect( dbname: 'sales' )
conn.exec( "SELECT * FROM pg_stat_activity" ) do |result|
  puts "     PID | User             | Query"
  result.each do |row|
    puts " %7d | %-16s | %s " %
      row.values_at('procpid', 'usename', 'current_query')
  end
end

Build Status

Build Status
Travis-CI Build Status Appveyor

Requirements

  • Ruby 1.9.3-p392, or 2.0.0-p0.

  • PostgreSQL 8.4.x or later (with headers, -dev packages, etc).

It may work with earlier versions of Ruby/PostgreSQL as well, but those are not regularly tested.

How To Install

Install via RubyGems:

gem install pg

You may need to specify the path to the 'pg_config' program installed with Postgres:

gem install pg -- --with-pg-config=<path to pg_config>

If you're installing via Bundler, you can provide compile hints like so:

bundle config build.pg --with-pg-config=<path to pg_config>

See README-OS_X.rdoc for more information about installing under MacOS X, and README-Windows.rdoc for Windows build/installation instructions.

There's also a Google+ group and a mailing list if you get stuck, or just want to chat about something.

Type Casts

Pg can optionally type cast result values and query parameters in Ruby or native C code. This can speed up data transfers to and from the database, because String allocations are reduced and conversions in (slower) Ruby code can be omitted.

Very basic type casting can be enabled by:

conn.type_map_for_results = PG::BasicTypeMapForResults.new conn
# ... this works for result value mapping:
conn.exec("select 1, now(), '{2,3}'::int[]").values
    # => [[1, 2014-09-21 20:51:56 +0200, [2, 3]]]

conn.type_map_for_queries = PG::BasicTypeMapForQueries.new conn
# ... and this for param value mapping:
conn.exec_params("SELECT $1::text, $2::text, $3::text", [1, 1.23, [2,3]]).values
    # => [["1", "1.2300000000000000E+00", "{2,3}"]]

But Pg's type casting is highly customizable. That's why it's divided into 2 layers:

Encoders / Decoders (ext/pg_*coder.c, lib/pg/*coder.rb)

This is the lower layer, containing encoding classes that convert Ruby objects for transmission to the DBMS and decoding classes to convert received data back to Ruby objects. The classes are namespaced according to their format and direction in PG::TextEncoder, PG::TextDecoder, PG::BinaryEncoder and PG::BinaryDecoder.

It is possible to assign a type OID, format code (text or binary) and optionally a name to an encoder or decoder object. It's also possible to build composite types by assigning an element encoder/decoder. PG::Coder objects can be used to set up a PG::TypeMap or alternatively to convert single values to/from their string representation.

PG::TypeMap and derivations (ext/pg_type_map*.c, lib/pg/type_map*.rb)

A TypeMap defines which value will be converted by which encoder/decoder. There are different type map strategies, implemented by several derivations of this class. They can be chosen and configured according to the particular needs for type casting. The default type map is PG::TypeMapAllStrings.

A type map can be assigned per connection or per query respectively per result set. Type maps can also be used for COPY in and out data streaming. See PG::Connection#copy_data .

Contributing

To report bugs, suggest features, or check out the source with Mercurial, check out the project page. If you prefer Git, there's also a Github mirror.

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests/specs, and generate the API documentation.

The current maintainers are Michael Granger <ged@FaerieMUD.org> and Lars Kanis <lars@greiz-reinsdorf.de>.

Copying

Copyright © 1997-2015 by the authors.

  • Jeff Davis <ruby-pg@j-davis.com>

  • Guy Decoux (ts) <decoux@moulon.inra.fr>

  • Michael Granger <ged@FaerieMUD.org>

  • Lars Kanis <lars@greiz-reinsdorf.de>

  • Dave Lee

  • Eiji Matsumoto <usagi@ruby.club.or.jp>

  • Yukihiro Matsumoto <matz@ruby-lang.org>

  • Noboru Saitou <noborus@netlab.jp>

You may redistribute this software under the same terms as Ruby itself; see www.ruby-lang.org/en/LICENSE.txt or the LICENSE file in the source for details.

Portions of the code are from the PostgreSQL project, and are distributed under the terms of the PostgreSQL license, included in the file POSTGRES.

Portions copyright LAIKA, Inc.

Acknowledgments

See Contributors.rdoc for the many additional fine people that have contributed to this library over the years.

We are thankful to the people at the ruby-list and ruby-dev mailing lists. And to the people who developed PostgreSQL.

Something went wrong with that request. Please try again.