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
8 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
Michael88's avatar

So shoud it be like this?

'options' => [
	'target_session_attrs' => 'read',
],
JussiMannisto's avatar

What are you trying to achieve by including it to begin with?

1 like
Michael88's avatar

When automatic failover swithces traffic from primary to replica I need connection in laravel to switch as well

1 like
JussiMannisto's avatar

I was asking why you added that option. It's not in the database config file by default. Did you read what it does from the Postgres docs?

You've set target_session_attrs to read-write for all connections, which means you cannot connect to the hosts you defined under read if they're read-only connections.

You also have a separate host array at the root level in addition to read and write hosts. I don't know Laravel interprets this, but it might mess things up. I recommend you read the docs before continuing.

1 like

Please or to participate in this conversation.