0

我有一个userdb包含 5 个模式的 PostgreSQL 数据库。

Schema 1- Persons
Schema 2- Project
Schema 3- Shop
Schema 4- Test

我能够使用pg_connect. 如何访问该数据库中的特定模式?

当数据库中只有一个模式时,我能够连接到模式。但是现在由于我有多个模式,我很难访问任何特定的模式。

 <?php
 // attempt a connection
 $dbh = pg_connect("host=**.****.*******.*** dbname=test user=merlin port=5433 password=passw123");
 if (!$dbh) {
     die("Error in connection test: " . pg_last_error());
 } 
// execute query
 $sql = "SELECT * FROM test.country";
 $result = pg_query($dbh, $sql);
 if (!$result) {
     die("Error in SQL query: " . pg_last_error());
 }       

 // iterate over result set
 // print each row
 while ($row = pg_fetch_array($result)) {
     echo "Country code: " . $row[0] . "<br />";
     echo "Country name: " . $row[1] . "<p />";
 }       

 // free memory
 pg_free_result($result);       

 // close connection
 pg_close($dbh);      
?>
4

3 回答 3

6

使用模式名称限定表

select *
from my_schema.aircraft
于 2013-04-19T17:22:22.640 回答
3

Schema-qualify the table name as Clodoaldo already advised. Or set the search_path for a permanent effect. It works much like a search path in the file system.

How permanent depends on how you set it. See:

于 2013-04-19T19:03:24.107 回答
3

使用:
SET search_path TO myschema;或者

将搜索路径设置为 myschema,myschemab;

https://www.postgresql.org/docs/9.4/static/ddl-schemas.html

于 2016-06-30T18:36:51.120 回答