Multi-Database Multi-схемы, похоже, не работают в более новых версиях postgresql - PullRequest
0 голосов
/ 02 июля 2019

У меня есть проблема, когда я хочу создать две базы данных и две схемы. Каждая база данных будет иметь отдельную схему. Общедоступная схема была удалена. Я пробовал это без падения публичная схема с тем же результатом. При подключении к базе данных с базой данных и владельцем схемы схема не может быть просмотрена или использована.

Я создал отдельные базы данных со схемой, но никогда не пытался настроить более одной. Проблема возникает из-за нескольких баз данных и схем.

create user tom;
create database fishes owner tom encoding = 'UTF8';
create schema fish authorization tom;
alter database fishes set schema 'fish';
create user harry;
create database lizards owner harry encoding = 'UTF8';
create schema lizard authorization harry;
alter database lizards set schema 'lizard';

psql (10.8)
postgres=# create user tom;
CREATE ROLE
postgres=# create database fishes owner tom encoding = 'UTF8';
CREATE DATABASE
postgres=# create schema fish authorization tom;
CREATE SCHEMA
postgres=# alter database fishes set schema 'fish';
ALTER DATABASE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 tom       |                                                            | {}


postgres=# \l
                                                 List of databases
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 fishes    | tom      | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
(4 rows)


postgres=# \dn
List of schemas
 Name | Owner
------+-------
 fish | tom
(1 row)
postgres=# create user harry;
CREATE ROLE
postgres=# create database lizards owner harry encoding = 'UTF8';
CREATE DATABASE
postgres=# create schema lizard authorization harry;
CREATE SCHEMA
postgres=# alter database lizards set schema 'lizard';
ALTER DATABASE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 harry     |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 tom       |                                                            | {}


postgres=# \l
                                                 List of databases
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 fishes    | tom      | UTF8     | English_United States.1252 | English_United States.1252 |
 lizards   | harry    | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
(5 rows)


postgres=# \dn
List of schemas
  Name  | Owner
--------+-------
 fish   | tom
 lizard | harry
(2 rows)

postgres=# alter user tom password  'xxx';
ALTER ROLE
postgres=# alter user harry password  'xxx';
ALTER ROLE

when I did a \dnpsql -U tom fishes
Password for user tom:
psql (10.8)
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

fishes=> \l
                                                 List of databases
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 fishes    | tom      | UTF8     | English_United States.1252 | English_United States.1252 |
 lizards   | harry    | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
           |          |          |                            |                            | postgres=CTc/postgres
(5 rows)


fishes=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 harry     |                                                            | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 tom       |                                                            | {}


fishes=> \dn
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 row)


fishes=> show search_path;
 search_path
-------------
 fish
(1 row)

fishes=> create table eels( type varchar(30) primary key );
ERROR:  no schema has been selected to create in
LINE 1: create table eels( type varchar(30) primary key );
                     ^
fishes=> create table fish.eels( type varchar(30) primary key );
ERROR:  schema "fish" does not exist
LINE 1: create table fish.eels( type varchar(30) primary key );
                     ^

Я ожидаю, что когда я войду в систему с любым из пользователей, когда я войду с одним из моих пользователей, не являющихся postgres, \ dn покажет схему для базы данных, которая будет показана с show search_path. Путь поиска правильный. Также кажется, что доступ не предоставлен владельцу схемы.

1 Ответ

0 голосов
/ 03 июля 2019

Ответ такой, как предложил Джереми в своем комментарии.

Перед созданием схемы подключитесь к базе данных, в которой вы хотите создать схему. Не существует синтаксиса создания схемы, включающей имя базы данных, которую следует создатьв, так что это нужно сделать следующим образом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...