当前位置:网站首页>PostgreSQL user role permissions
PostgreSQL user role permissions
2022-06-25 19:08:00 【Daiyuanpei】
stay PostgreSQL in , All the content revolves around role Concept building .
stay macOS First installation on PostgreSQL when , The script Use your macOS The user name creates a role , And granted the permission list .
PostgreSQL There are no users in , Only role .
By running... In the terminal psql postgres , You will use your macOS The user name automatically logs in to PostgreSQL, To access the created roles .
As far as I'm concerned , Created flaviocopes role , have access to \du Command to see it :

notice ? By default , I have the following Character attributes :
SuperuserCreate roleCreate DBReplicationBypass RLS
And I am not a member of any other role ( We'll talk about it later )
Create a new character (Creating a new role)
Use CREATE ROLE Command to create a new role :
CREATE ROLE <role>;for example :
CREATE ROLE testing;
We have a Cannot login New roles for role properties . Our newly created users will not be able to log in .
You can enter \q command , And then you type in psql postgres -U testing Try , But you will see this error :

To solve this problem , We must add... At creation time LOGIN Character attributes :
CREATE ROLE <role> WITH LOGIN;If we delete the role using the following method :
DROP ROLE <role>; And this time add WITH LOGIN :
DROP ROLE testing;
CREATE ROLE testing WITH LOGIN; We can see testing Roles can log in , Because this time we did not Cannot login role attribute :

Try adding a command \q sign out , Then add psql postgres -U testing :

Please note that , Tips From =# Change to => Because we don't have Superuser Character attributes .
Add password for role (Adding a password to a role)
In the last CREATE ROLE In command , We created a role without a password . Of course , Have ( Security ) Passwords are very important . You can use PASSWORD Keyword add password :
CREATE ROLE <role> WITH LOGIN PASSWORD '<password>';Create user (CREATE USER)
Use automatically added LOGIN Property to define a role ( Effectively create users who can log in ) It's using CREATE USER :
CREATE USER <role> PASSWORD '<password>';Add role attributes to the role (Adding a role attribute to a role)
Available later ALTER ROLE Command to add a role attribute to a role .
Suppose we create one that does not LOGIN The role of attributes :
CREATE ROLE <username> PASSWORD '<password>';We can add it using the following methods :
ALTER ROLE <role> WITH LOGIN;Built in role attributes (Built-in role attributes)
We've seen LOGIN Character attributes , To allow the role to log in .
however , What other built-in character properties can we use ?
LOGIN/NOLOGIN:allow ( Or not allowed ) Log in to PostgreSQLSUPERUSER/NOSUPERUSER:allow ( Or not allowed ) Superuser rights . The database superuser will bypass the exceptionLOGIN( Must be granted separately ) Other permission checks .CREATEDB/NOCREATEDB:allow ( Or not allowed ) The ability to create new databasesCREATEROLE/NOCREATEROLE:allow ( Or not allowed ) Create a new characterCREATEUSER/NOCREATEUSER:allow ( Or not allowed ) Create a new userINHERIT/NOINHERIT:allow ( Or not allowed ) Make privileges inheritableREPLICATION/NOREPLICATION:grant ( Or not granted ) Copy permission ( Advanced topics that we will not cover )
Group roles (Group roles)
stay PostgreSQL in , No user groups .
contrary , You can create roles with specific permissions , Then grant these roles to other roles .
If the role has INHERIT attribute , Then the roles will inherit the permissions granted to their roles .
Create group roles (Create a group role)
To create a group role , Please type the
CREATE ROLE <groupname>;The syntax is the same as creating roles .
After creating the group role , You can use GRANT Add roles to group roles :
GRANT <groupname> TO <role> for example , We can create one flavio User roles , One “ Employee ” Group roles , And assign the user to the group role :
CREATE USER flavio PASSWORD 'superSecret123$';
CREATE ROLE employee;
GRANT employee TO flavio;You can delete a role from a group role using the following methods :
REVOKE <groupname> FROM <username>example :
REVOKE employee FROM flavio;Group role properties (Group role attributes)
By default , Add roles to group roles Can't Make the role inherit the properties of the group role ( jurisdiction ).
You need to use INHERIT Property to create a group role .
Suppose you create an employee group role , And assigned CREATEDB attribute :
CREATE ROLE employee WITH CREATEDB INHERIT; Now use INHERIT Create a new character :
CREATE ROLE flavio;
GRANT employee TO flavio;
Translated from : https://flaviocopes.com/postgres-user-permissions/
边栏推荐
- 广州华锐互动打造VR展厅全景在线虚拟展厅
- TCP/IP 测试题(四)
- Training of long and difficult sentences in postgraduate entrance examination day90
- 一晚上做了一个xpath终结者:xpath-helper-plus
- 云上弹性高性能计算,支持生命科学产业高速发展、降本增效
- TCP/IP 测试题(五)
- How to quickly close port 8080
- 网络安全检测与防范 测试题(二)
- 五、HikariCP源码分析之初始化分析二
- Detailed explanation of oauth2 - Introduction (I)
猜你喜欢
![Analysis of China's road freight volume, market scale and competition pattern in 2020 [figure]](/img/93/fd2cfa315c2f6d232078f7b20a7eb1.jpg)
Analysis of China's road freight volume, market scale and competition pattern in 2020 [figure]

Uncover ges super large scale graph computing engine hyg: Graph Segmentation
![[elt.zip] openharmony paper Club - memory compression for data intensive applications](/img/54/8248c1d95b04498d44a00ea94a6c85.png)
[elt.zip] openharmony paper Club - memory compression for data intensive applications

Leetcode-101-symmetric binary tree

Redis configuration (Internet access, password)

一晚上做了一个xpath终结者:xpath-helper-plus

Apifox简单了解——WEB端测试的集大成者

LeetCode-101-对称二叉树

两轮市场红海,利尔达芯智行如何乘风破浪?

华为发布两大昇腾计划 推动AI人才发展和科研创新
随机推荐
Tcp/ip test questions (V)
Huawei released two promotion plans to promote AI talent development and scientific research innovation
Gbpnzd firm offer for 14 months, simulation for 19 months, test stable
Analysis of China's road freight volume, market scale and competition pattern in 2020 [figure]
初探Oracle全栈虚拟机---GraalVM
Idea common plug-ins
Training of long and difficult sentences in postgraduate entrance examination day90
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]
Ali visual AI training camp -day03- construction of electronic photo album (face and expression recognition)
Cutting feet to fit shoes - talking about the ramp reconstruction on the track
Uncover ges super large scale graph computing engine hyg: Graph Segmentation
Current situation of China's hydraulic cylinder industry in 2020 (with application fields, policies and regulations, supply and demand status and enterprise pattern) [figure]
Laravel validation rule followed Role of auth:: id()
Guangzhou Sinovel interactive VR panorama brings development to all walks of life
Miner's Diary: why should I go mining on April 5, 2021
【历史上的今天】6 月 25 日:笔记本之父诞生;Windows 98 发布;通用产品代码首次商用
On Oracle full stack virtual machine -- graalvm
mysql视图讲解
One night I worked as an XPath Terminator: XPath Helper Plus
Divine reversion EA