Пользователи PostgreSQL
Введение | |
Список пользователей | |
Создать пользователя (role) | |
Добавить пользователю прав (или роли атрибутов) | |
Изменить пароль | |
Похожие статьи |
Введение
Пользователи
Получить список пользователей
postgres=# \du
List of roles Role name | Attributes | Member of -----------+------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication | {}
Создать пользователя
Чтобы создать пользователя выполните
postgres=# CREATE USER user01 WITH PASSWORD 'pwd01';
CREATE ROLE
Проверка
postgres=# \du
List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} user01 | | {}
Добавить пользователю прав
Правильнее сказать добавить роли атрибутов, так как в таблице пользователей они выглядят так
\du
List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- andrei | | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
Как видите, у пользователя (роли) andrei никаких атрибутов нет. Добавим командой
postgres=# ALTER ROLE andrei WITH SUPERUSER CREATEDB CREATEROLE REPLICATION BYPASSRLS;
ALTER ROLE
Проверка
postgres=# \du
List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- andrei | Superuser, Create role, Create DB, Replication, Bypass RLS | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
Изменить пароль
Чтобы изменить пароль пользователя выполните
sudo -u postgres psql -c "alter user ИМЯ_ПОЛЬЗОВАТЕЛЯ with encrypted password НОВЫЙ_ПАРОЛЬ"
Если вы вызываете команду из Python
f"sudo -u postgres psql -c \"alter user {dbuser} with encrypted password '{dbpass}'\"