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

MrTomek's avatar

PostgreSql relationships between tables in different schemas

Can I do relationships between tables in different schemas?

0 likes
6 replies
gregrobson's avatar

I don't think (although I might be wrong) that you can just set the $table value to include a different schema? e.g. $table = 'schema_alt.foos'.

What I have done in the past is alter my config to include a new connection - same database, different schema.

'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' => '',
    'schema' => 'schema_main', // <----- Primary schema
    'sslmode' => 'prefer',
],

// Same database, different default schema
'pgsql_alt' => [
    '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' => '',
    'schema' => 'schema_alt', // <----- Alternative Schema
    'sslmode' => 'prefer',
],

Then for models that need to reference the alternative schema:

class Foo extends Model
{
    $connection = 'pgsql_alt';

    // table will now become "schema_alt.foos" as it uses the second connection.
}

Of course, if you have a lot of schemas this might become a bit of a hassle to manage! The query builder will always allow you to join across schemas with far less hassle.

MrTomek's avatar

How can I do this with query builder?

MrTomek's avatar

With protected $table = 'admin.kategorie_kursow'; I get Illuminate\Database\QueryException with message 'could not find driver (SQL: select * from "admin"."kategorie_kursow")'

MrTomek's avatar

I can't even select

'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' => '',
            'schema' => 'admin',
            'sslmode' => 'prefer',
        ],

Illuminate\Database\QueryException with message 'could not find driver (SQL: select * from "kategorie_kursow")'

gregrobson's avatar

If you output the contents of your phpinfo, is there a section labelled "pdo_pgsql"? If not, you might need to locate your php.ini file and uncomment the line where your pdo_pgsql driver is loaded.

<?php
phpinfo();
MrTomek's avatar

Yeah I have this

pdo_pgsql

PDO Driver for PostgreSQL   enabled
PostgreSQL(libpq) Version   9.6.6
Module version  7.1.12-2+0~20171207160618.12+stretch~1.gbp5c91f3
Revision    $Id: 93712a6af603ebb2ee5792c5be271d4d03edfbde $

And I have another problem expected authentication request from server, but received b (SQL: select * from "test") when Test::all()

Please or to participate in this conversation.