• Stars
    star
    749
  • Rank 58,200 (Top 2 %)
  • Language
    C
  • License
    Other
  • Created almost 11 years ago
  • Updated 26 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PostgreSQL client library for Ruby

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 9.3 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('pid', 'usename', 'query')
    end
  end

Build Status

Build Status Github Actions Binary gems Build Status Appveyor

Requirements

  • Ruby 2.5 or newer
  • PostgreSQL 9.3.x or later (with headers, -dev packages, etc).

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

Versioning

We tag and release gems according to the Semantic Versioning principle.

As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision.

For example:

  spec.add_dependency 'pg', '~> 1.0'

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.

If you want to install as a signed gem, the public certs of the gem signers can be found in the certs directory of the repository.

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.

The following PostgreSQL column types are supported by ruby-pg (TE = Text Encoder, TD = Text Decoder, BE = Binary Encoder, BD = Binary Decoder):

  • Integer: TE, TD, BD 💡 No links? Switch to here 💡
    • BE: Int2, Int4, Int8
  • Float: TE, TD, BD
    • BE: Float4, Float8
  • Numeric: TE, TD
  • Boolean: TE, TD, BE, BD
  • String: TE, TD, BE, BD
  • Bytea: TE, TD, BE, BD
  • Base64: TE, TD, BE, BD
  • Timestamp:
    • TE: local, UTC, with-TZ
    • TD: local, UTC, UTC-to-local
    • BE: local, UTC
    • BD: local, UTC, UTC-to-local
  • Date: TE, TD, BE, BD
  • JSON and JSONB: TE, TD
  • Inet: TE, TD
  • Array: TE, TD
  • Composite Type (also called "Row" or "Record"): TE, TD

The following text and binary formats can also be encoded although they are not used as column type:

  • COPY input and output data: TE, TD, BE, BD
  • Literal for insertion into SQL string: TE
  • SQL-Identifier: TE, TD

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 .

The following base type maps are available:

  • PG::TypeMapAllStrings - encodes and decodes all values to and from strings (default)
  • PG::TypeMapByClass - selects encoder based on the class of the value to be sent
  • PG::TypeMapByColumn - selects encoder and decoder by column order
  • PG::TypeMapByOid - selects decoder by PostgreSQL type OID
  • PG::TypeMapInRuby - define a custom type map in ruby

The following type maps are prefilled with type mappings from the PG::BasicTypeRegistry :

  • PG::BasicTypeMapForResults - a PG::TypeMapByOid prefilled with decoders for common PostgreSQL column types
  • PG::BasicTypeMapBasedOnResult - a PG::TypeMapByOid prefilled with encoders for common PostgreSQL column types
  • PG::BasicTypeMapForQueries - a PG::TypeMapByClass prefilled with encoders for common Ruby value classes

Thread support

PG is thread safe in such a way that different threads can use different PG::Connection objects concurrently. However it is not safe to access any Pg objects simultaneously from more than one thread. So make sure to open a new database server connection for every new thread or use a wrapper library like ActiveRecord that manages connections in a thread safe way.

If messages like the following are printed to stderr, you're probably using one connection from several threads:

message type 0x31 arrived from server while idle
message type 0x32 arrived from server while idle
message type 0x54 arrived from server while idle
message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle

Fiber IO scheduler support

Pg is fully compatible with Fiber.scheduler introduced in Ruby-3.0 since pg-1.3.0. On Windows support for Fiber.scheduler is available on Ruby-3.1 or newer. All possibly blocking IO operations are routed through the Fiber.scheduler if one is registered for the running thread. That is why pg internally uses the asynchronous libpq interface even for synchronous/blocking method calls. It also uses Ruby's DNS resolution instead of libpq's builtin functions.

Internally Pg always uses the nonblocking connection mode of libpq. It then behaves like running in blocking mode but ensures, that all blocking IO is handled in Ruby through a possibly registered Fiber.scheduler. When PG::Connection.setnonblocking(true) is called then the nonblocking state stays enabled, but the additional handling of blocking states is disabled, so that the calling program has to handle blocking states on its own.

An exception to this rule are the methods for large objects like PG::Connection#lo_create and authentication methods using external libraries (like GSSAPI authentication). They are not compatible with Fiber.scheduler, so that blocking states are not passed to the registered IO scheduler. That means the operation will work properly, but IO waiting states can not be used to switch to another Fiber doing IO.

Ractor support

Pg is fully compatible with Ractor introduced in Ruby-3.0 since pg-1.5.0. All type en/decoders and type maps are shareable between ractors if they are made frozen by Ractor.make_shareable. Also frozen PG::Result and PG::Tuple objects can be shared. All frozen objects (except PG::Connection) can still be used to do communication with the PostgreSQL server or to read retrieved data.

PG::Connection is not shareable and must be created within each Ractor to establish a dedicated connection.

Contributing

To report bugs, suggest features, or check out the source with Git, check out the project page.

After checking out the source, install all dependencies:

$ bundle install

Cleanup extension files, packaging files, test databases. Run this to change between PostgreSQL versions:

$ rake clean

Compile extension:

$ rake compile

Run tests/specs on the PostgreSQL version that pg_config --bindir points to:

$ rake test

Or run a specific test per file and line number on a specific PostgreSQL version:

$ PATH=/usr/lib/postgresql/14/bin:$PATH rspec -Ilib -fd spec/pg/connection_spec.rb:455

Generate the API documentation:

$ rake docs

Make sure, that all bugs and new features are verified by tests.

The current maintainers are Michael Granger [email protected] and Lars Kanis [email protected].

Copying

Copyright (c) 1997-2022 by the authors.

You may redistribute this software under the same terms as Ruby itself; see https://www.ruby-lang.org/en/about/license.txt or the BSDL 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.

More Repositories

1

linguistics

A generic, language-neutral framework for extending Ruby objects with linguistic methods.
Ruby
273
star
2

ruby-wordnet

A Ruby interface to the WordNet® Lexical Database.
Ruby
136
star
3

bluecloth

A Ruby implementation of Markdown
Ruby
79
star
4

linkparser

A high-level interface to the CMU Link Grammar. (Github mirror)
C
76
star
5

darkfish

A project to make a complete replacement for the default HTML generator for Rdoc, the API documentation-extraction system for Ruby. (github mirror)
Ruby
35
star
6

sysexits

Exit status codes for Ruby system programs.
Ruby
32
star
7

aurelia-semantic-ui

A collection of Semantic UI custom elements for Aurelia applications (Git mirror)
JavaScript
23
star
8

rdoc-generator-fivefish

A(nother) HTML generator for RDoc
Ruby
22
star
9

rspec-formatter-webkit

A webkit-aware pretty formatter for RSpec (github mirror).
Ruby
20
star
10

io-reactor

A multiplexed, single-threaded, event-driven IO reactor
Ruby
18
star
11

treequel

An LDAP toolkit for Ruby, intended to allow quick, easy access to LDAP directories in a manner consistent with LDAP's hierarchical, free-form nature.
Ruby
14
star
12

configurability

A composable configuration system for Ruby (Github mirror)
Ruby
12
star
13

chione

An Entity/Component System for Ruby inspired by Artemis (git mirror)
Ruby
10
star
14

loggability

A composable Ruby logging system built on the standard Logger library.
Ruby
9
star
15

ruby-ode

A Ruby binding for the Open Dynamics Engine™
C
7
star
16

ruby-pf

An experimental Ruby interface to `pf`, the OpenBSD Packet Filter (github mirror).
Ruby
7
star
17

inversion

Inversion is a templating system for Ruby. It uses the "Inversion of Control" principle to decouple the contents and structure of templates from the code that uses them, making it easier to separate concerns, keep your tests simple, and avoid polluting scopes with ephemeral data. (github mirror)
Ruby
7
star
18

ruby-openldap

A simple, but full-featured Ruby binding for libldap (Github mirror)
C
6
star
19

pluginfactory

A Ruby module for adding plugin-like behavior to classes
Ruby
6
star
20

redleaf

A better Ruby binding for the Redland RDF library (librdf)
Ruby
6
star
21

thingfish

An extensible digital asset manager.
Ruby
4
star
22

RDoc.tmbundle

A TextMate 2 bundle for editing Ruby API documentation
4
star
23

ruby-axis

A Ruby library for accessing Axis Communications network cameras. (git mirror)
Ruby
4
star
24

foreman-export-daemontools

An exporter for Foreman that exports supervise service directories
Ruby
4
star
25

arborist

Arborist is a monitoring framework that follows the UNIX philosophy of small parts and loose coupling for stability, reliability, and customizability.
Ruby
4
star
26

gemserver

The Github mirror of an experimental minimalist Rubygems index and gem server written in Sinatra.
JavaScript
4
star
27

mues

An experimental MORPG engine, written in Ruby. It's unfinished, and hasn't been touched in several years, but maybe still has some interesting stuff. I use https://github.com/ged/chione now instead.
Ruby
4
star
28

drbservice

SSL-encrypted, authenticated DRb service library (Github mirror)
Ruby
3
star
29

rake-deveiate

A collection of Rake tasks common to all my projects (Github mirror)
Ruby
3
star
30

ruby-verse

A Ruby binding for Verse, a network protocol that lets multiple applications act together as one large application by sharing data over a network. It's still a work in progress.
C
3
star
31

arrow

A Ruby web application framework for Apache and mod_ruby
Ruby
3
star
32

rdtool

RD is Ruby's POD. RDtool is a formatter for RD. With this fork, I'm intending to package rdtool as a gem, and update it as necessary for later versions of Ruby.
Ruby
3
star
33

cztop-reactor

An implementation of the Reactor pattern for ZeroMQ sockets (git mirror)
Ruby
3
star
34

strelka-metriks

Metriks support for Strelka web apps (github mirror)
Ruby
2
star
35

hglib

A Ruby client library for the Mercurial distributed revision control system (Github mirror)
Ruby
2
star
36

schedulability

A composable scheduling library for Ruby (git mirror)
Ruby
2
star
37

homebrew-faeriemud

A Homebrew tap for installing FaerieMUD development dependencies
Ruby
2
star
38

geds-rake-tasklibs

A collection of rake task libraries I use in a number of different projects. It's a git mirror of my main Mercurial repo.
Ruby
2
star
39

hoe-highline

A Hoe plugin for building interactive Rake tasks. (Github mirror)
Ruby
2
star
40

hoe-deveiate

A collection of Rake tasks and utility functions I use to maintain my Open Source projects. It's really only useful if you want to help maintain one of them.
Ruby
2
star
41

hoe-mercurial

A Mercurial plugin for Hoe (Github mirror)
Ruby
2
star
42

symphony

An asynchronous job system.
Ruby
2
star
43

devEiate.tmbundle

A dark TextMate theme with balanced, subdued colors
2
star
44

strelka-fancyerrors

A Strelka plugin for rendering a bunch of useful information on error responses suitable for developers
Ruby
2
star
45

fluent_fixtures

A toolkit for building a collection of composable testing fixtures with a fluent interface. (git mirror)
Ruby
2
star
46

hoe-manualgen

A Hoe plugin for generating a manual, tutorial, cookbook, or other in-depth documentation.
JavaScript
2
star
47

Mercurial-Manager

A Ruby implementation of mercurial-server (Git mirror)
Ruby
2
star
48

ruby-framenet

Git mirror of a Ruby library for FrameNet
Ruby
2
star
49

ronin-shell

This is an experimental object-oriented command shell, in the same vein as rush or Windows PowerShell. (Github mirror)
Ruby
2
star
50

ft2-ruby

Freetype2 bindings for Ruby, updated for Ruby 1.9.2.
C
2
star
51

LSystem

A Ruby toolkit for constructing and using Lindenmeyer systems
Ruby
2
star
52

strelka-app-profiler

Github mirror of the profiler plugin for Strelka applications
Ruby
1
star
53

ged

It's like a .plan, only different
1
star
54

assemblage

Git mirror of the Assemblage continuous integration toolkit
Ruby
1
star
55

assemblage-web

Web services for the Assemblage continuous integration toolkit (Git mirror)
Ruby
1
star
56

pluggability

Add pluggability to any Ruby base class
Ruby
1
star
57

rdoc-generator-sixfish

Git mirror of https://hg.sr.ht/~ged/Sixfish
SCSS
1
star
58

strelka

A Ruby web framework for Mongrel2 (mirror)
Ruby
1
star
59

strelka-newrelic

New Relic instrumentation middleware for Strelka apps
Ruby
1
star
60

ming-ruby

A resurrected (and updated) copy of the Ruby binding for libming
Ruby
1
star
61

RSpec3.tmbundle

An RSpec 3 bundle. You probably won't like it.
Ruby
1
star
62

git-gist-viewer

An Aurelia app used to search users and view their Github information
JavaScript
1
star
63

thingfish-metastore-pg

PostgreSQL metastore backend for Thingfish (git mirror)
Ruby
1
star
64

state_machines-sequel

A Sequel adapter for the state_machines library
Ruby
1
star
65

blockchain

A playground for learning wtf a blockchain is
Ruby
1
star
66

ruby-mongrel2

Github mirror of Ruby-Mongrel2
Ruby
1
star
67

pushdown

A pushdown automaton toolkit for Ruby.
Ruby
1
star
68

homebrew-nginx-unit

Homebrew tap for Nginx Unit experimentation
Ruby
1
star
69

thingfish-processor-mp3

Git mirror of a basic mp3-processor plugin for the Thingfish digital asset manager
Ruby
1
star
70

thingfish-datastore-filesystem

Git mirror
Ruby
1
star
71

arborist-webservice

Git mirror of mercurial repo
Ruby
1
star
72

arborist-dns

DNS monitors and node types for Arborist
Ruby
1
star
73

newznabr

A Ruby port of Newsnab (http://www.newznab.com/), a Usenet indexer.
1
star
74

arborist-web

An experimental webservices implementation for Arborist
Ruby
1
star
75

ruby-ant-wireless

Ruby gem for ANT wireless on the Garmin ANT Stick
C
1
star
76

sequel-inlineschema

Github mirror of the inline schema plugin for Sequel
Ruby
1
star
77

strelka-presenters

Github mirror of the Strelka Presenters plugin
Ruby
1
star
78

aurelia-bailiwick

Aurelia adapters for the Bailiwick domain model toolkit (git mirror)
JavaScript
1
star
79

observability

A git mirror of the Observability library
Ruby
1
star
80

saltpack-ruby

A Ruby implementation of Saltpack, a modern crypto messaging format based on Dan Bernstein's NaCl (git mirror)
Ruby
1
star
81

ruby-zyre

A Ruby (MRI) binding for the Zyre library for reliable group messaging over local area networks, an implementation of the ZeroMQ Realtime Exchange protocol.
Ruby
1
star
82

strelka-cors

Cross-Origin Resource Sharing for Strelka applications
Ruby
1
star
83

tnetstrings.info

Git mirror for
JavaScript
1
star
84

bailiwick

A more domain-ish Javascript model toolkit (git mirror)
JavaScript
1
star
85

thingfish-processor-image

Github mirror of a basic image-processor plugin for the Thingfish digital asset manager
Ruby
1
star
86

strelka-authprovider-authtoken

An authentication provider plugin for Strelka applications.
Ruby
1
star
87

aurelia-semantic-ui-demo

Demo application/documentation for aurelia-semantic-ui (Github mirror)
HTML
1
star