pg is the Ruby interface to PostgreSQL, providing a low-level API for connecting to and interacting with PostgreSQL databases. As the standard PostgreSQL adapter for Ruby, it handles database connections, query execution, and result processing with minimal overhead. The gem wraps the libpq C library, offering direct access to PostgreSQL's native features while maintaining Ruby's ease of use.
The gem provides both synchronous and asynchronous query execution, prepared statements, and comprehensive support for PostgreSQL's advanced data types including arrays, JSON, hstore, and geometric types. It handles connection pooling, SSL connections, and COPY operations for bulk data transfer. The pg gem also includes detailed error reporting and notices from the PostgreSQL server, giving developers precise feedback about query execution and database state.
pg serves as the foundation for higher-level database libraries like ActiveRecord and Sequel when working with PostgreSQL. It's essential for applications requiring direct database access, custom query optimization, or PostgreSQL-specific features not exposed by ORM layers. The gem's performance characteristics and comprehensive PostgreSQL support make it the standard choice for Ruby applications backed by PostgreSQL databases.
gem install pg Run this gem instantly in your browser without any installation:
Open in RunRuby.dev →All 104 versions available for installation