database

created onJanuary 8, 2022

creating a database with encoding UTF-8 with a specified user as owner

postgres=# create database sampledb encoding 'UTF8' owner alice; CREATE DATABASE

grant permissions on databases and database objects

grant permission to connect to a database

postgres=# grant connect on database sampledb to bob; GRANT

grant all privileges on a database to a specified user (role). The ‘privileges’ keyword is optional in PostgreSQL but mandatory in strict SQL.

postgres=# grant all privileges on database sampledb to bob; GRANT

may be a bit omnicidal. You might want to fine tune the priviledges.

The general form to grant priviledges to database objects is

grant <priviledge> on <database object> <database object name> to <user|role>

The database objects used most often and the possible priviledges for their use are (I’m not going to use EBNF here):

table

grant

  • select
  • insert
  • update
  • delete
  • truncate
  • drop

on table table-name
on all tables in schema schema-name

sequence

grant

  • usage
  • select
  • update
  • drop

on sequence sequence-name
on all sequences in schema schema-name

note that in order to get the next value of a sequence, i.e. in JPA when persisting entities, you need the priviledge select.

database

grant

  • create
  • connect
  • drop

on database database-name

schema

grant

  • create
  • usage

on schema schema-name