Postgresql 修改数据库默认当前 schema

1,611 阅读1分钟

这是我参与8月更文挑战的第5天,活动详情查看:8月更文挑战

数据 d1 原默认当前 schema 为 public

--d1数据库下有4个模式,建表时不指定模式,表存在 public 模式下。
postgres=# \c d1 u1
You are now connected to database "d1" as user "u1".
d1=> \dn
  List of schemas
  Name  |  Owner   
--------+----------
 d1s1   | u1
 d1s2   | u1
 d1s3   | u2
 public | postgres
(4 rows)
d1=> \dt
Did not find any relations.
d1=> create table t1(id int);
CREATE TABLE
d1=> \dt
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 public | t1   | table | u1

客户端工具连接数据库查看更为明显,加粗的 schema 为 public,即为当前 schema image.png

修改数据库 d1 默认当前 schema 为 d1s1

--修改后建表不时,不指定模式,表存在 d1s1 模式下。
postgres=# alter database d1 set search_path to d1s1;
ALTER DATABASE
postgres=# \c d1 u1
You are now connected to database "d1" as user "u1".
d1=> create table t2(id int);
CREATE TABLE
d1=> \dt
       List of relations
 Schema | Name | Type  | Owner 
--------+------+-------+-------
 d1s1   | t2   | table | u1
(1 rows)

客户端工具连接数据库查看更为明显,加粗的 schema 为 d1s1,即为当前 schema image.png