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.
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.
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.
Official sources
- [1]pg v1.7.0 README(2026-10-04)
- [2]pg v1.7.0 changelog(2026-10-04)
- [3]pg v1.7.0 gemspec(2026-10-04)
- [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
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
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
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}"'
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
- License
- BSD-2-Clause
- Repository last updated
- Oct 2, 2026
Related information
Write a related articleShare a guide or use case for this OSS in Markdown. Articles are published after administrator approval.
Explore next
- DBeaver51,949 Stars
1 shared tag(s) · 1 shared category(s)
operate many database systems from one desktop SQL workspace
Java - DuckDB41,889 Stars
1 shared tag(s) · 1 shared category(s)
Run analytical SQL directly over CSV, Parquet, and DataFrames inside the application process without a database server
C++ - Directus38,015 Stars
1 shared tag(s) · 1 shared category(s)
layer APIs, a visual Studio, and AI/MCP on top of SQL databases
TypeScript - MongoDB28,614 Stars
1 shared tag(s) · 1 shared category(s)
a database server for document data and sharding
C++ - Turso Database24,557 Stars
1 shared tag(s) · 1 shared category(s)
run SQLite-compatible SQL on a Rust database engine with bindings for multiple languages
Rust - Neon23,170 Stars
1 shared tag(s) · 1 shared category(s)
Decouple PostgreSQL compute from storage for branching and elastic database workflows
Rust
Report incorrect information
Tell us if any listing information is incorrect or outdated.