Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael88's avatar

Connect to postgres cluster

Hello Could anyone please help me with pgsql cluster issue

I used this config

'pgsql' => [
        'driver' => 'pgsql',
        'host' => [
            env('DB_HOST_1', '10.0.0.1'),
            env('DB_HOST_2', '10.0.0.2'),
        ],
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'options' => [
            'target_session_attrs' => 'read-write',
        ],
        'read' => [
            'host' => [
                env('DB_HOST_1', '10.0.0.1'),
                env('DB_HOST_2', '10.0.0.2'),
            ],
        ],
        'write' => [
            'host' => env('DB_HOST_MASTER_WRITE', '10.0.0.1'),
        ],
        'sticky' => true,
        'charset' => 'utf8',
        'prefix' => '',
        'schema' => 'public',
        'sslmode' => 'prefer',
    ]

But as soon as master switches to replica it fails What can I do here?

Thank you so much in advance

1 like
3 replies
JussiMannisto's avatar

@vincent15000 Master (a.k.a. primary) and replica (a.k.a. slave) are basic concepts in database replication. Replica servers replicate data from primary server(s). The old terms are master/slave, the modern terms are primary/replica.

@Michael88 What does "it fails" mean? What actually happens?

And what do you mean by master switching to replica? Laravel uses a read connection by default. It only switches to the write connection when you write something to the DB. And all subsequent reads also use the write connection since you have the sticky option set (which is good).

1 like
JussiMannisto's avatar

You have this piece of config:

'options' => [
	'target_session_attrs' => 'read-write',
],

Why is it used?

You haven't overridden target_session_attrs for the read connections, so I think they also have the read-write attribute, although I haven't tested it. And since those are read-only connections, the connections should fail.

1 like

Please or to participate in this conversation.