To set the default schema, you'll have to execute a query like:
$conn = Foo::Conecction();
$conn->exec('SET search_path TO yourschema');
or, if you want to go about it in a more user-specific way:
$conn->exec('ALTER USER user SET search_path TO yourschema');
As a side-note: Please don't create your PDO instances like that (as the return value of a static method). PDO
offers a clean API right out of the box. You're not allowing the caller to determine what DB to connect to, instead, you're hard coding the credentials. This is considered bad practice by any standard. Consider passing the connection to wherever you need it, or -if you insist- create a method that at least requires the caller to pass DB credentials themselves.
I'd also recommend you include the collation in your DSN string, and possibly set some attributes to make life easier when debugging:
$pdo = new PDO(
"pgsql:dbname=$dbname;host=$host;port=5432;charset=utf8",
$user,
$pass,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,//set PDO to throw exceptions on error
PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,//NULL values are returned as PHP null's
]
);
Check out other attributes and use accordingly.
Last thing: Some time ago PDO
could occasionally run into trouble resolving the localhost
host-name. I suspect this bug has been fixed already, but just in case it crops up (or even: hasn't been fixed), I'd recommend you use ip addresses whenever you can. If you know the IP already, there's no real point in having to bother the DNS server with resolving a string that you know will resolve to 127.0.0.1
anyway.