我有一个具有多个模式的postgres数据库。当我用psql从shell连接到数据库并运行\dt时,它使用默认的公共连接模式。是否有可以指定的标志或如何更改模式?
当前回答
在PostgreSQL中,系统通过遵循搜索路径来确定哪个表,该路径是要查找的模式列表。
搜索路径中的第一个匹配表将被视为需要的表,否则,如果没有匹配则引发错误,即使匹配表名称存在于数据库中的其他模式中。
要显示当前搜索路径,您可以使用以下命令:
SHOW search_path;
要把新的模式放到路径中,你可以使用:
SET search_path TO myschema;
或者如果你想要多个模式:
SET search_path TO myschema, public;
参考:https://www.postgresql.org/docs/current/static/ddl-schemas.html
其他回答
这是旧的,但我把导出放在我的别名连接到db:
alias schema_one.con="PGOPTIONS='--search_path=schema_one' psql -h host -U user -d database etc"
对于另一个schema:
alias schema_two.con="PGOPTIONS='--search_path=schema_two' psql -h host -U user -d database etc"
如果你使用PSQL,只需输入
SET schema 'temp';
在此之后,\d显示“temp”中的所有关系
快速解决方案可以是:
SELECT your_db_column_name from "your_db_schema_name"."your_db_tabel_name";
在psql命令中使用带句点的模式名获取该模式的信息。
设置:
test=# create schema test_schema;
CREATE SCHEMA
test=# create table test_schema.test_table (id int);
CREATE TABLE
test=# create table test_schema.test_table_2 (id int);
CREATE TABLE
显示test_schema中的关系列表:
test=# \dt test_schema.
List of relations
Schema | Name | Type | Owner
-------------+--------------+-------+----------
test_schema | test_table | table | postgres
test_schema | test_table_2 | table | postgres
(2 rows)
显示test_schema。test_table定义:
test=# \d test_schema.test_table
Table "test_schema.test_table"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
显示test_schema中的所有表:
test=# \d test_schema.
Table "test_schema.test_table"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
Table "test_schema.test_table_2"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
等等……
在PostgreSQL中,系统通过遵循搜索路径来确定哪个表,该路径是要查找的模式列表。
搜索路径中的第一个匹配表将被视为需要的表,否则,如果没有匹配则引发错误,即使匹配表名称存在于数据库中的其他模式中。
要显示当前搜索路径,您可以使用以下命令:
SHOW search_path;
要把新的模式放到路径中,你可以使用:
SET search_path TO myschema;
或者如果你想要多个模式:
SET search_path TO myschema, public;
参考:https://www.postgresql.org/docs/current/static/ddl-schemas.html
推荐文章
- 将varchar字段的类型更改为整数:"不能自动转换为整数类型"
- PostgreSQL可以索引数组列吗?
- PostgreSQL:角色不允许登录
- 如何查找Postgres / PostgreSQL表及其索引的磁盘大小
- 是使用各有一个模式的多个数据库更好,还是使用一个数据库有多个模式更好?
- 如果字符串包含
- 对于PostgreSQL表来说,多大才算太大?
- 将一列的多个结果行连接为一列,按另一列分组
- 使用pgadmin连接到heroku数据库
- 在PostgreSQL中快速发现表的行数
- 更改varchar列的大小为较低的长度
- 如何首次配置postgresql ?
- 数据库性能调优有哪些资源?
- 如何在PostgreSQL中自动更新时间戳
- 当使用JDBC连接到postgres时,是否可以指定模式?