programing

psql을 사용할 때 postgres에서 스키마를 선택하는 방법은 무엇입니까?

lastcode 2023. 5. 2. 22:43
반응형

psql을 사용할 때 postgres에서 스키마를 선택하는 방법은 무엇입니까?

여러 스키마가 있는 포스트그레스 데이터베이스가 있습니다.셸에서 데이터베이스에 연결할 때psql그리고 나는 달립니다.\dt공용인 기본 연결 스키마를 사용합니다.지정할 수 있는 플래그가 있거나 스키마를 변경하려면 어떻게 해야 합니까?

포스트그레에서SQL 시스템은 검색할 스키마 목록인 검색 경로를 따라 의미하는 테이블을 결정합니다.

검색 경로의 첫 번째 일치 테이블은 원하는 테이블로 간주됩니다. 그렇지 않으면 일치 테이블 이름이 데이터베이스의 다른 스키마에 있더라도 일치하지 않으면 오류가 발생합니다.

다음 명령을 사용하여 현재 검색 경로를 표시할 수 있습니다.

SHOW search_path;

경로에 새 스키마를 추가하려면 다음을 사용할 수 있습니다.

SET search_path TO myschema;

또는 다중 스키마를 원하는 경우:

SET search_path TO myschema, public;

참조: https://www.postgresql.org/docs/current/static/ddl-schemas.html

\l - Display database
\c - Connect to database
\dn - List schemas
\dt - List tables inside public schemas
\dt schema1. - List tables inside particular schemas. For eg: 'schema1'.

데이터베이스를 변경하시겠습니까?

\l - to display databases
\c - connect to new database

갱신하다.

당신의 질문을 다시 읽었습니다.스키마를 표시하는 방법

\dn - list of schemas

스키마를 변경하려면 다음을 시도할 수 있습니다.

SET search_path TO

사용하는 경우psql딱 맞는 활자

SET schema 'temp';

그리고 그 후 \d는 모든 관계를 보여줍니다.temp

이 스키마에 대한 정보를 가져오려면 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 | 

기타...

이것은 오래된 것이지만, 나는 데이터베이스에 연결하기 위해 내 별칭에 내보내기를 넣었습니다.

alias schema_one.con="PGOPTIONS='--search_path=schema_one' psql -h host -U user -d database etc"

다른 스키마의 경우:

alias schema_two.con="PGOPTIONS='--search_path=schema_two' psql -h host -U user -d database etc"

키워드:

SET search_path TO

예:

SET search_path TO your_schema_name;

빠른 해결책은 다음과 같습니다.

SELECT your_db_column_name from "your_db_schema_name"."your_db_tabel_name";

PostgreSQL 14 데비안

    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 스키마의 뷰를 보는 등

도커 내부에서 psql을 가지고 노는 경우 다음과 같이 실행합니다.

docker exec -e "PGOPTIONS=--search_path=<your_schema>" -it docker_pg psql -U user db_name

언급URL : https://stackoverflow.com/questions/34098326/how-to-select-a-schema-in-postgres-when-using-psql

반응형