PHPackages                             voryx/pgasync - 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. voryx/pgasync

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

voryx/pgasync
=============

Async Reactive Postgres Driver for PHP (Non-blocking)

2.1.0(2mo ago)11294.0k↓24.1%24[4 issues](https://github.com/voryx/PgAsync/issues)[1 PRs](https://github.com/voryx/PgAsync/pulls)4MITPHPPHP &gt;=7.0.0CI passing

Since Dec 10Pushed 2mo ago9 watchersCompare

[ Source](https://github.com/voryx/PgAsync)[ Packagist](https://packagist.org/packages/voryx/pgasync)[ RSS](/packages/voryx-pgasync/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (19)Used By (4)

[![Build Status](https://camo.githubusercontent.com/9f729182c0c06070d6d9ff8d01ab65931fc3e6101312bf05f8a8a2a5aa109453/68747470733a2f2f7472617669732d63692e6f72672f766f7279782f50674173796e632e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/voryx/PgAsync)

PgAsync
=======

[](#pgasync)

Asynchronous Reactive Postgres Library for PHP (Non-blocking)

What it is
----------

[](#what-it-is)

This is an asynchronous Postgres library for PHP. Observables are returned by the query methods allowing asynchronous row-by-row data handling (and other Rx operators on the data) See [Rx.PHP](https://github.com/asm89/Rx.PHP). Network and event processing is handled by [ReactPHP](http://reactphp.org/).

This is a pure PHP implementation (you don't need Postgres extensions to use it).

Example - Simple Query
----------------------

[](#example---simple-query)

```
$client = new PgAsync\Client([
    "host" => "127.0.0.1",
    "port" => "5432",
    "user"     => "matt",
    "database" => "matt"
]);

$client->query('SELECT * FROM channel')->subscribe(
    function ($row) {
        var_dump($row);
    },
    function ($e) {
        echo "Failed.\n";
    },
    function () {
        echo "Complete.\n";
    }
);
```

Example - parameterized query
-----------------------------

[](#example---parameterized-query)

```
$client = new PgAsync\Client([
     "host" => "127.0.0.1",
     "port" => "5432",
     "user"     => "matt",
     "database" => "matt",
     "auto_disconnect" => true //This option will force the client to disconnect as soon as it completes.  The connection will not be returned to the connection pool.

]);

$client->executeStatement('SELECT * FROM channel WHERE id = $1', ['5'])
    ->subscribe(
        function ($row) {
            var_dump($row);
        },
        function ($e) {
            echo "Failed.\n";
        },
        function () {
            echo "Complete.\n";
        }
    );
```

Example - LISTEN/NOTIFY
-----------------------

[](#example---listennotify)

```
$client = new PgAsync\Client([
     "host" => "127.0.0.1",
     "port" => "5432",
     "user"     => "matt",
     "database" => "matt"
]);

$client->listen('some_channel')
    ->subscribe(function (\PgAsync\Message\NotificationResponse $message) {
        echo $message->getChannelName() . ': ' . $message->getPayload() . "\n";
    });

$client->query("NOTIFY some_channel, 'Hello World'")->subscribe();
```

Example - Connecting over TLS with CA certificate file
------------------------------------------------------

[](#example---connecting-over-tls-with-ca-certificate-file)

```
$client = new PgAsync\Client([
    "host"                => "127.0.0.1",
    "port"                => "5432",
    "user"                => "matt",
    "database"            => "matt",
    "tls"                 => "verify-full",
    "tls_connector_flags" => [
        "cafile" => "/path/to/ca.crt",
    ],
]);

$client->query('SELECT * FROM channel')->subscribe(
    function ($row) {
        var_dump($row);
    },
    function ($e) {
        echo "Failed.\n";
    },
    function () {
        echo "Complete.\n";
    }
);
```

Install
-------

[](#install)

With [composer](https://getcomposer.org/) install into you project with:

Install pgasync: `composer require voryx/pgasync`

What it can do
--------------

[](#what-it-can-do)

- Run queries (CREATE, UPDATE, INSERT, SELECT, DELETE)
- Queue commands
- Return results asynchronously (using Observables - you get data one row at a time as it comes from the db server)
- Prepared statements (as parameterized queries)
- Connection pooling (basic pooling)

What it can't quite do yet
--------------------------

[](#what-it-cant-quite-do-yet)

- Transactions (Actually though, just grab a connection and you can run your transaction on that single connection)

What's next
-----------

[](#whats-next)

- Add more testing
- Transactions
- Take over the world

Keep in mind
------------

[](#keep-in-mind)

This is an asynchronous library. If you begin 3 queries (subscribe to their observable):

```
$client->query("SELECT * FROM table1")->subscribe(...);
$client->query("SELECT * FROM table2")->subscribe(...);
$client->query("SELECT * FROM table3")->subscribe(...);
```

It will start all of them almost simultaneously (and you will begin receiving rows on all 3 before any of them have completed). This can be great if you want to run 3 queries at the same time, but if you have some queries that need information that was modified by other statements, this can cause a race condition:

```
$client->query("INSERT INTO invoices(inv_no, customer_id, amount) VALUES('1234A', 1, 35.75)")->subscribe(...);
$client->query("SELECT SUM(amount) AS balance FROM invoices WHERE customer_id = 1")->subscribe(...);
```

In the above situation, your balance may or may not include the invoice inserted on the first line.

You can avoid this by using the Rx concat\* operator to only start up the second observable after the first has completed:

```
$insert = $client->query("INSERT INTO invoices(inv_no, customer_id, amount) VALUES('1234A', 1, 35.75)");
$select = $client->query("SELECT SUM(amount) AS balance FROM invoices WHERE customer_id = 1");

$insert
    ->concat($select)
    ->subscribe(...);
```

Testing
-------

[](#testing)

We use docker to run a postgresql instance for testing. To run locally, just install docker and run the following command from the project root:

```
docker-compose -f docker/docker-compose.yml up -d
```

If you need to reset the database, just stop the docker instance and delete the `docker/database` directory. Restart the docker with the above command and it will initialize the database again.

The tests do not change the ending structure of the database, so you should not normally need to do this.

###  Health Score

59

—

FairBetter than 99% of packages

Maintenance82

Actively maintained with recent releases

Popularity47

Moderate usage in the ecosystem

Community28

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 77.8% 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 ~266 days

Recently: every ~604 days

Total

15

Last Release

84d ago

Major Versions

0.1.2 → 1.0.02016-08-26

1.0.3 → 2.0.02017-08-01

### Community

Maintainers

![](https://www.gravatar.com/avatar/7d3dd76986135ec7771465f6e0e187478b3d4b50ed4bde14da7e27db84c35f14?d=identicon)[mbonneau](/maintainers/mbonneau)

---

Top Contributors

[![mbonneau](https://avatars.githubusercontent.com/u/748287?v=4)](https://github.com/mbonneau "mbonneau (105 commits)")[![davidwdan](https://avatars.githubusercontent.com/u/4969183?v=4)](https://github.com/davidwdan "davidwdan (17 commits)")[![WyriHaximus](https://avatars.githubusercontent.com/u/147145?v=4)](https://github.com/WyriHaximus "WyriHaximus (5 commits)")[![zwirek](https://avatars.githubusercontent.com/u/7045479?v=4)](https://github.com/zwirek "zwirek (4 commits)")[![samnela](https://avatars.githubusercontent.com/u/1852108?v=4)](https://github.com/samnela "samnela (1 commits)")[![leda-ferreira](https://avatars.githubusercontent.com/u/7317891?v=4)](https://github.com/leda-ferreira "leda-ferreira (1 commits)")[![aaronbonneau](https://avatars.githubusercontent.com/u/8574061?v=4)](https://github.com/aaronbonneau "aaronbonneau (1 commits)")[![Tatikoma](https://avatars.githubusercontent.com/u/1888609?v=4)](https://github.com/Tatikoma "Tatikoma (1 commits)")

---

Tags

asyncpostgresqlpostgrespgsqldriverreactreactiverx.php

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/voryx-pgasync/health.svg)

```
[![Health](https://phpackages.com/badges/voryx-pgasync/health.svg)](https://phpackages.com/packages/voryx-pgasync)
```

###  Alternatives

[doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.

9.7k578.4M5.6k](/packages/doctrine-dbal)[martin-georgiev/postgresql-for-doctrine

Extends Doctrine with native PostgreSQL support for arrays, JSONB, ranges, PostGIS geometries, text search, ltree, uuid, and 100+ PostgreSQL-specific functions.

4485.3M4](/packages/martin-georgiev-postgresql-for-doctrine)[aura/sql

A PDO extension that provides lazy connections, array quoting, query profiling, value binding, and convenience methods for common fetch styles. Because it extends PDO, existing code that uses PDO can use this without any changes to the existing code.

5632.5M43](/packages/aura-sql)[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4572.9M34](/packages/aura-sqlquery)[scienta/doctrine-json-functions

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

58723.9M36](/packages/scienta-doctrine-json-functions)[react/mysql

Async MySQL database client for ReactPHP.

340421.0k29](/packages/react-mysql)

PHPackages © 2026

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