乐闻世界logo
搜索文章和话题

What is the purpose of the LOGIN attribute in PostgreSQL roles?

1个答案

1

In PostgreSQL, roles are used to manage database access permissions, similar to traditional user accounts. Roles can have various attributes, including the LOGIN attribute, which specifies whether a role is allowed to log in to the database.

Specifically, if a role has the LOGIN attribute, it can be used as a login account for the database. If the role lacks the LOGIN attribute, even if it is granted other permissions (such as access to specific database objects), it cannot be used directly to log in to the database. This means that to create an account for a person or application that can log in to the database, you must ensure the role has the LOGIN attribute.

For example, suppose we have a database and we need to create a role for the finance department that requires logging into the database to access specific financial reports. We can create this role as follows:

sql
CREATE ROLE finance LOGIN PASSWORD 'secure_password';

Here, finance is the role name, LOGIN indicates to PostgreSQL that this role can be used for database login, and PASSWORD specifies the password required for authentication.

On the other hand, if we want to create a role for managing database permissions without allowing direct login, we can omit the LOGIN attribute:

sql
CREATE ROLE admin NOLOGIN;

This means the admin role cannot be used directly to log in to the database, but it can be used to grant permissions to other login roles or perform administrative tasks.

In summary, the LOGIN attribute is a key attribute that controls whether a role can log in to the database. It is important to decide whether to grant the LOGIN attribute when creating roles based on specific requirements.

2024年7月26日 14:47 回复

你的答案