{"id":1345,"date":"2023-10-13T12:50:53","date_gmt":"2023-10-13T09:50:53","guid":{"rendered":"https:\/\/blog.geckich.space\/?p=1345"},"modified":"2023-10-13T12:50:53","modified_gmt":"2023-10-13T09:50:53","slug":"how-to-add-read-only-user-to-postgresql-9-linux","status":"publish","type":"post","link":"https:\/\/geckich.space\/?p=1345","title":{"rendered":"How to add read only user to PostgreSQL (9+, Linux)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Login into postgres terminal first. It is important to specify database name you want to grant permissions to.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>su postgres -c \"\/usr\/pgsql-15\/bin\/psql your_database\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Than create new user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE ROLE readonly_user LOGIN ENCRYPTED PASSWORD 'readonly_password';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then grant permissions to select from all tables in database that have already been created:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT CONNECT ON DATABASE your_database TO readonly_user;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly_user;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s all.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want read only user to automatically receive select permissions on newly created tables in future, than execute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO readonly_user;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Sources:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>https:\/\/stackoverflow.com\/questions\/760210\/how-do-you-create-a-read-only-user-in-postgresql<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Login into postgres terminal first. It is important to specify database name you want to grant permissions to. Than create new user: Then grant permissions to select from all tables in database that have already been created: That&#8217;s all. If you want read only user to automatically receive select permissions on newly created tables in &hellip; <a href=\"https:\/\/geckich.space\/?p=1345\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to add read only user to PostgreSQL (9+, Linux)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10,7],"tags":[25,199,48,198,200],"class_list":["post-1345","post","type-post","status-publish","format-standard","hentry","category-linux","category-sysadmin","tag-console","tag-database","tag-linux","tag-postgresql","tag-psql"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts\/1345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1345"}],"version-history":[{"count":1,"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts\/1345\/revisions"}],"predecessor-version":[{"id":1346,"href":"https:\/\/geckich.space\/index.php?rest_route=\/wp\/v2\/posts\/1345\/revisions\/1346"}],"wp:attachment":[{"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geckich.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}