Swoft PostgreSQL

curtis18/swoft-pgsql is a simple wrapper for PostgreSQL in Svoft and supports connection pool configuration.

Github

installation

curtis18/swoft-pgsql requires Swoole's swoole/ext-postgresql extension. As an additional extension to Svoft, you need to install it manually:

  • With the composer command:
 composer require curtis18/swoft-pgsql 
  • Configured by composer.json :
     "curtis18/swoft-pgsql": "~1.0.0" 

use

Basic configuration

The PostgreSQL configuration for the Swoft application is in the configuration file app/bean.php.

 use Swoft\Pgsql\PgsqlDb;

'pgsql'          => [
    'class'    => PgsqlDb::class,
    'host'     => '127.0.0.1',
    'port'     => 5432,
    'database' => 'dbname',
    'schema'   => ['postgis', 'public'],
    'charset'  => 'utf8',
    'user'     => 'username',
    'password' => 'pass'
], 
  • Class specifies the current configuration driver type
  • Host connection address default 127.0.0.1
  • Port port default 5432
  • Database connection database default postgres

Connection pool configuration

All connection pool configurations are similar in Swoft. The configuration is in the configuration file app/bean.php. The default connection pool name is pgsql.pool.

 'pgsql.pool'      => [
    'class'     => \Swoft\Pgsql\Pool::class,
    'pgsqlDb'   => bean('pgsql'),
    'minActive' => 2,
    'mixActive' => 20,
    'maxWait'   => 0,
    'maxWaitTime' => 0,
    'maxIdleTime' => 60,
], 

Note: Each worker will create the same connection pool. Not as many as possible, the parameter configuration is based on the machine configuration and the number of workers.

Use connection pool

 <?php declare(strict_types=1);

namespace App\Model\Data;

use Swoft\Pgsql\Pool;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Config\Annotation\Mapping\Config;

/**
 * Class Test
 *
 * @since 2.0
 * 
 * @Bean()
 */
class Test

    /**
     * @Inject("pgsql.pool")
     * @var Pool
     */
    private $pgsql;

    public function getTest(): array
    {
        $connection = $this->pgsql->createConnection();
        return $connection->select("SELECT * FROM test;");
    }
} 

Participate in the contribution

Welcome to participate in the contribution, you can

/docs/2.x/en/extra/postgresql.html
progress-bar