2

I have an app that access multiple databases on a single PostgreSQL server. I liked very much the Microsoft SQL Server concept where you create a CREATE LOGIN app_user WITH PASSWORD 'xxx' on the server-level. Then, you create CREATE USER app_user WITH LOGIN app_user on database level.

You manage security issues like password retention, disable/enable user etc on server-level (which is handy feature if the database count is more than 20)

Is this doable also on PostgreSQL?

Thanks a lot!

asked Apr 25, 2021 at 6:23
1
  • 2
    Users/roles are also valid per server in Postgres. Commented Apr 25, 2021 at 6:35

1 Answer 1

3

Postgres works differently, but you can achieve the same.

There is no distinction between a "user" and a "login".

Postgres only has roles - a role with the "login" privilege is commonly referred to as a "user" (see the manual for create user)

Access to a database is granted based on the connect privilege that enables a user (=a role with the login privilege) to connect to a specific database.

By default newly created users can connect to any database in the instance (aka "cluster") because the role public has the privilege connect granted by default. This privilege is granted per database.

If you want to allow every created user to connect to every database, there is nothing you need to do - this is the default. Just create the users (note that those users won't be able to do anything meaningful with the databases as long as they don't they get additional privileges to access or create objects in those databases).

If you want more fine grained control, remove the connect privilege of the public role for every database.

Then grant the connect privilege to the roles (users) you want to allow to connect to a specific database.


There is a second level of access control based on the host based authentication and controlled through editing the pg_bha.conf. I prefer opening up all access for regular users (on the internal network of course) through pg_hba.conf and controlling concrete access through SQL and explicit GRANTs - this is a bit easier to manager as you don't need to edit a server side file. But this is very much a matter of personal preferences. If you need other level of access control e.g. based on the client IP protocol or authentication method, then pg_hba.conf is the only way to do that.

answered Apr 25, 2021 at 7:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.