OSS TanbouSign in with GitHub

connect Ruby applications to PostgreSQL with queries, COPY, type casting, and asynchronous libpq I/O

About these scores

OSS scale score is an unbounded metric that log-compresses and weights Stars, Watchers, Forks, and Contributors. Discovery score is the current OSS scale score minus the score at discovery. Update pace is commits in the last 30 days, growth momentum is the OSS scale score difference within the recent observation window, and OSS health is a 0–100 rating based on available recency, Community Health, and release data.

Stars
869
Primary language
C
License
BSD-2-Clause
Repository last updated
Oct 2, 2026
On this page

Overview

pg is the native Ruby interface to PostgreSQL's libpq. It provides connection and result APIs, text and binary type maps, COPY support, parameterized queries, and Fiber-scheduler-aware asynchronous I/O, and is commonly used underneath Ruby and Rails PostgreSQL stacks.

Features and best fit

Based on official documentation; not hands-on tested · Content checked:

Key features

Query PostgreSQL through PG::Connection and PG::Result

The gem exposes core connection, parameterized-query, and result-iteration APIs and supports PostgreSQL 10 and later.

Sources: [1][3]

Control data conversion with text/binary coders and type maps

Encoders and decoders cover integers, timestamps, JSON/JSONB, arrays, records, COPY data, and other formats, while multiple TypeMap strategies control query and result conversion.

Sources: [1]

Integrate with Fiber.scheduler through nonblocking libpq I/O

pg internally uses libpq's nonblocking connection mode and routes potentially blocking I/O through a registered Ruby Fiber scheduler where supported.

Sources: [1]

Best fit

Fits Ruby and Rails applications using PostgreSQL directly or through an ORM

It works both as a low-level PostgreSQL client and as the driver beneath higher-level connection management such as ActiveRecord.

Sources: [1]

Before adoption

v1.7.0 no longer supports Ruby below 3.1

Version 1.7.0 removes Ruby 2.7 and 3.0 compatibility, and the gemspec requires Ruby 3.1 or newer. Binary gems bundle libpq, while source-gem builds require libpq headers and development packages.

Sources: [2][3][1]

Do not concurrently operate on the same PG object from multiple threads or fibers

The README permits concurrent use of separate PG::Connection objects but warns against simultaneous access to the same PG object. Version 1.7.0 also fixes SQL injection and several overflow and memory-safety issues.

Sources: [1][2]

Official sources

  1. [1]pg v1.7.0 README(2026-10-04)
  2. [2]pg v1.7.0 changelog(2026-10-04)
  3. [3]pg v1.7.0 gemspec(2026-10-04)
  4. [4]pg BSD 2-Clause license(2026-10-04)
Supplemental curator note

Version 1.7.0 drops Ruby below 3.1. Binary gems bundle libpq, while source builds require libpq development headers. Avoid simultaneous access to the same PG object from multiple threads or fibers.

Try it in 3 steps

  1. 1

    Create an isolated Bundler project for pg 1.7.0

    Pin pg to 1.7.0 in a Ruby 3.1+ environment.

    mkdir -p ruby-pg-demo && printf 'source "https://rubygems.org"\ngem "pg", "1.7.0"\n' > ruby-pg-demo/Gemfile
  2. 2

    Install the pg gem

    Supported platforms normally use a binary gem; source-gem builds require libpq development headers and related build tools.

    cd ruby-pg-demo && bundle install
  3. 3

    Print pg and libpq versions without connecting to a database

    Verify the loaded gem and bundled or system libpq version without opening a PostgreSQL connection.

    cd ruby-pg-demo && bundle exec ruby -rpg -e 'puts "pg=#{PG::VERSION}"; puts "libpq=#{PG.library_version}"'
Check the official README

Growth

Growth trends · Last 30 days

869 Stars

Trend data is still being collected.

Development activity

Last 90 days · weekly

Commits (last 30 days)
28
Open PRs
6

Development activity is still being collected.

Built with

Categories and tags

Categories

GitHub data

GitHub dataView detailed GitHub data

GitHub Topics

  • ruby
  • database
  • postgresql
  • rdbms
  • client
  • c
Stars
869
Forks
200
Watchers
28
Open issues
10
Contributors
61
Owner type
User
Primary language
C
Repository last updated
Oct 2, 2026
Write a related article

Share a guide or use case for this OSS in Markdown. Articles are published after administrator approval.

Report incorrect information

Tell us if any listing information is incorrect or outdated.

After reading this page, do you know what to do next?