'How to provide read only access to all existing databases in postgresql

what's the recommended way to provide readonly access to all databases in postgresql version 12.

I am using this found at How do you create a read-only user in PostgreSQL?

CREATE ROLE readaccess;
CREATE USER my_user_test WITH PASSWORD 'my_user_test';

GRANT readaccess TO my_user_test;
\c database_name;

-- need to connect to the database first on which we need to execute the below commands
GRANT CONNECT ON DATABASE database_name TO readaccess;
GRANT USAGE ON SCHEMA public TO readaccess;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO readaccess;

-- Assign permissions to read all newly tables created in the future
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readaccess; 

But using this approach I need to individually connect to each database and provide the role read only access.

Is there a better approach to provide a read only access all existing databases at once?

Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source