我有一个具有多个模式的postgres数据库。当我用psql从shell连接到数据库并运行\dt时,它使用默认的公共连接模式。是否有可以指定的标志或如何更改模式?


当前回答

这是旧的,但我把导出放在我的别名连接到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"

其他回答

快速解决方案可以是:

SELECT your_db_column_name from "your_db_schema_name"."your_db_tabel_name";

如果你使用PSQL,只需输入

SET schema 'temp';

在此之后,\d显示“temp”中的所有关系

关键词:

SET search_path TO

例子:

SET search_path TO your_schema_name;

PostgreSQL 14 Debian

    postgres@ovhswift:~$ psql
psql (14.0 (Debian 14.0-1.pgdg100+1))
Type "help" for help.

postgres=# create database test;
CREATE DATABASE
postgres=# \c test
You are now connected to database "test" as user "postgres".
test=# create schema tests;
CREATE SCHEMA
test=# \dt
Did not find any relations.
test=# create table pubtable (id integer);
CREATE TABLE
test=# create table tests.schematable (id integer);
CREATE TABLE
test=# \dt
          List of relations
 Schema |   Name   | Type  |  Owner
--------+----------+-------+----------
 public | pubtable | table | postgres
(1 row)

test=# \dt tests.
Did not find any relation named "tests.".
test=# \dt tests
Did not find any relation named "tests".
test=# \dt 'tests.'
Did not find any relation named "tests.".
test=# \dt 'tests.*'
            List of relations
 Schema |    Name     | Type  |  Owner
--------+-------------+-------+----------
 tests  | schematable | table | postgres
(1 row)

test=# \dt 'tests*'
Did not find any relation named "tests*".
test=# \dt 'tests.*'
            List of relations
 Schema |    Name     | Type  |  Owner
--------+-------------+-------+----------
 tests  | schematable | table | postgres
(1 row)

对于\dv等,同样可以查看模式中的视图

这是旧的,但我把导出放在我的别名连接到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"