【发布时间】:2019-11-13 11:48:26
【问题描述】:
我想创建两个数据库时遇到问题 和两个模式。每个数据库都有一个单独的模式。 公共架构已被删除。我试过这个没有放弃 具有相同结果的公共模式。使用数据库和架构所有者连接到数据库时,无法看到或使用架构。
我已经使用架构设置了单个数据库,但从未尝试设置多个。问题似乎来自多个数据库和架构。
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 显示的数据库架构。 Search_path 是正确的。此外,似乎没有授予架构所有者访问权限。
【问题讨论】:
-
看起来你是在你第一次连接的任何数据库中创建模式。创建 fishes 数据库后,您需要在创建架构之前连接到该数据库。
-
谢谢你,你的建议很有效。
标签: postgresql schema