Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgres: create a database with an owner already set #57

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions roles/postgres/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,17 @@
enabled: true
become: true

- name: Setup postgresql databases
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be executed after we create a user, or otherwise setting a database owner will fail due to non-existent role.

community.postgresql.postgresql_db:
name: "{{ item.database }}"
with_items: "{{ postgres_users }}"
become: true
become_user: postgres

- name: Setup postgresql users
community.postgresql.postgresql_user:
db: "{{ item.database }}"
Copy link
Member Author

@malor malor Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was useless, as the option controls which database to connect to, and not which database to grant permissions on. We connect as admin role postgres to database postgres, and that is sufficient to create user roles and databases.

user: "{{ item.username }}"
with_items: "{{ postgres_users }}"
become: true
become_user: postgres

- name: Grant users permissions to create tables in the schema `public`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now think that my fix in 3ccb652 was wrong an I misunderstood what changed in PostgreSQL 15. The database owner will have the necessary permissions on the schema public, and no additional grants are needed (https://www.enterprisedb.com/blog/new-public-schema-permissions-postgresql-15).

community.postgresql.postgresql_privs:
db: "{{ item.database }}"
privs: CREATE
type: schema
objs: public
role: "{{ item.username }}"
- name: Setup postgresql databases
community.postgresql.postgresql_db:
name: "{{ item.database }}"
owner: "{{ item.username }}"
with_items: "{{ postgres_users }}"
become: true
become_user: postgres
Expand Down
Loading