PHPackages                             t1nkl/postgres-pgbouncer-extension - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. t1nkl/postgres-pgbouncer-extension

ActiveLibrary[Database &amp; ORM](/categories/database)

t1nkl/postgres-pgbouncer-extension
==================================

Laravel PostgreSQL pgbouncer extension fix for PDO::ATTR\_EMULATE\_PREPARES option

1.1.0(6mo ago)765.4k—3.2%2[1 issues](https://github.com/t1nkl/postgres-pgbouncer-extension/issues)MITPHPPHP ^8.0

Since Jun 14Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/t1nkl/postgres-pgbouncer-extension)[ Packagist](https://packagist.org/packages/t1nkl/postgres-pgbouncer-extension)[ RSS](/packages/t1nkl-postgres-pgbouncer-extension/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (7)Used By (0)

Laravel PostgreSQL + PgBouncer extension (fix for PDO::ATTR\_EMULATE\_PREPARES)
===============================================================================

[](#laravel-postgresql--pgbouncer-extension-fix-for-pdoattr_emulate_prepares)

A tiny Laravel package that makes working with PostgreSQL through PgBouncer safe and predictable when `PDO::ATTR_EMULATE_PREPARES` is enabled.

Why you might need it:

- PgBouncer in transaction/session pooling mode does not support server‑side prepared statements.
- The common workaround is to enable PDO emulated prepares (`PDO::ATTR_EMULATE_PREPARES => true`).
- With emulation enabled, default binding behavior in Laravel/PDO can cause booleans and some types to be sent in ways PgBouncer/PostgreSQL may not expect.

This package provides a custom `PostgresConnection` that:

- Formats `DateTimeInterface` values using your connection grammar date format.
- Converts PHP booleans to the PostgreSQL-native textual values `'true'` and `'false'` during binding.
- Binds values using explicit PDO parameter types: integers as `PDO::PARAM_INT`, resources as `PDO::PARAM_LOB`, and everything else as `PDO::PARAM_STR` for consistency with emulated prepares.

The package is automatically discovered by Laravel and is only activated for the `pgsql` connection when `PDO::ATTR_EMULATE_PREPARES` is present and enabled in your connection `options`.

---

Requirements
------------

[](#requirements)

- PHP: ^8.0
- Laravel Framework: ^10.0 | ^11.0 | ^12.0
- PostgreSQL + PgBouncer (transaction or session pooling scenarios)

Installation
------------

[](#installation)

```
composer require t1nkl/postgres-pgbouncer-extension
```

Laravel package auto-discovery will register the service provider; no manual configuration is needed beyond the database option below.

Configuration
-------------

[](#configuration)

Enable emulated prepares on your PostgreSQL connection. You can declare it either as an associative map or as a numeric flag — this package supports both styles.

`config/database.php`

```
'connections' => [
    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'charset' => 'utf8',
        'prefix' => '',
        'search_path' => 'public',
        'sslmode' => 'prefer',

        // EITHER associative option style (recommended):
        'options' => [
            PDO::ATTR_EMULATE_PREPARES => true,
        ],

        // OR numeric option style (also supported):
        // 'options' => [
        //     PDO::ATTR_EMULATE_PREPARES,
        // ],
    ],
],
```

Make sure your default connection is `pgsql` or that you add the `options` to the specific PostgreSQL connection you actually use.

If you modify configuration, remember to clear caches:

```
php artisan config:clear
php artisan cache:clear
```

How it works
------------

[](#how-it-works)

- The service provider hooks into Laravel’s connection resolver for `pgsql` only when `PDO::ATTR_EMULATE_PREPARES` is detected in the connection `options`.
- It swaps the default `Illuminate\Database\PostgresConnection` with `PostgresPgbouncerExtension\Database\PostgresConnection`.
- The custom connection overrides:
    - `prepareBindings`: converts `DateTimeInterface` to the grammar date format and casts booleans to `'true'`/`'false'` strings.
    - `bindValues`: binds integers as `PDO::PARAM_INT`, resources as `PDO::PARAM_LOB`, and all other values as `PDO::PARAM_STR` to keep behavior stable with emulated prepares.

Usage
-----

[](#usage)

There is nothing special to do. Once installed and the option is enabled, all queries through the `pgsql` connection will use the safer binding strategy.

Example: boolean filters are sent as `'true'`/`'false'` which PostgreSQL parses natively.

```
User::where('is_active', true)->get();
```

Troubleshooting
---------------

[](#troubleshooting)

- The behavior does not change:
    - Ensure `PDO::ATTR_EMULATE_PREPARES` is present in your `options` and set to `true` (or listed numerically).
    - Confirm your app uses the `pgsql` connection you configured (`config('database.default')`).
    - Clear config/cache (`php artisan config:clear && php artisan cache:clear`).
    - Make sure the package is installed and not excluded from auto-discovery.
- Still seeing unexpected parameter types?
    - Double-check any custom connection/resolver code that might override this package.
    - Try a minimal repro calling the query builder and logging SQL + bindings.

Testing locally
---------------

[](#testing-locally)

This repository includes a test suite. To run it:

```
composer install
composer test
```

Related work
------------

[](#related-work)

Original gist/package inspiration:

License
-------

[](#license)

MIT License. See `LICENSE` for details.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance64

Regular maintenance activity

Popularity38

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~247 days

Recently: every ~308 days

Total

6

Last Release

196d ago

PHP version history (2 changes)1.0.0PHP ^7.4|^8.0|^8.1

1.1.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fbe1fcaa74d383612f418f93152bae06da41404b8b63e91940eea4e7a00dbfa1?d=identicon)[t1nkl](/maintainers/t1nkl)

---

Top Contributors

[![t1nkl](https://avatars.githubusercontent.com/u/10539665?v=4)](https://github.com/t1nkl "t1nkl (9 commits)")

---

Tags

laravellaravel-packagepdopdo-pgsqlpgbouncerpostgrespostgresqllaraveldatabasepostgresqlpostgrespgbouncerATTR\_EMULATE\_PREPARES

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/t1nkl-postgres-pgbouncer-extension/health.svg)

```
[![Health](https://phpackages.com/badges/t1nkl-postgres-pgbouncer-extension/health.svg)](https://phpackages.com/packages/t1nkl-postgres-pgbouncer-extension)
```

###  Alternatives

[scienta/doctrine-json-functions

A set of extensions to Doctrine that add support for json query functions.

58723.9M36](/packages/scienta-doctrine-json-functions)[umbrellio/laravel-pg-extensions

Extensions for Postgres Laravel

102426.5k1](/packages/umbrellio-laravel-pg-extensions)[umbrellio/laravel-ltree

Extension LTree (Postgres) for Laravel

34111.6k](/packages/umbrellio-laravel-ltree)[asmiarowski/laravel-postgres

Eloquent support for postgreSQL fields

1638.1k1](/packages/asmiarowski-laravel-postgres)[orptech/laravel-migration-partition

Laravel extensions that extends Illuminate to enable partitioned table creation within Laravel migrations.

3426.7k](/packages/orptech-laravel-migration-partition)[jrsaunders/shard-matrix

A Complete Database Sharding system for MYSQL and/or Postgres. Using Laravels Query Builder easily scale up your application. Configure your whole solution in one Yaml Config file.

271.5k](/packages/jrsaunders-shard-matrix)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
