> echo "CREATE DATABASE testdb;" | psql -h localhost -d postgres -U dbadmin -W Password: CREATE DATABASE > echo "GRANT CONNECT ON DATABASE testdb TO dbuser;" | psql -h localhost -d testdb -U dbadmin -W > echo "GRANT USAGE, CREATE ON SCHEMA public TO dbuser;" | psql -h localhost -d testdb -U dbadmin -W > echo "GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO dbuser;" | psql -h localhost -d testdb -U dbadmin -W > echo "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO dbuser;" | psql -h localhost -d testdb -U dbadmin -W > echo "GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO dbuser;" | psql -h localhost -d testdb -U dbadmin -W > echo "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT USAGE, SELECT ON SEQUENCES TO dbuser;" | psql -h localhost -d testdb -U dbadmin -W > echo -e '\du\n\l\n' | sudo -u postgres psql List of roles Role name | Attributes -----------+------------------------------------------------------------ dbadmin | Create DB dbuser | postgres | Superuser, Create role, Create DB, Replication, Bypass RLS List of databases Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges -----------+----------+----------+-----------------+---------+---------+------------+-----------+----------------------- postgres | postgres | UTF8 | libc | C.UTF-8 | C.UTF-8 | | | template0 | postgres | UTF8 | libc | C.UTF-8 | C.UTF-8 | | | =c/postgres + | | | | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | libc | C.UTF-8 | C.UTF-8 | | | =c/postgres + | | | | | | | | postgres=CTc/postgres testdb | dbadmin | UTF8 | libc | C.UTF-8 | C.UTF-8 | | | =Tc/dbadmin + | | | | | | | | dbadmin=CTc/dbadmin + | | | | | | | | dbuser=c/dbadmin (4 rows) > echo 'CREATE TABLE testtab (id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, code VARCHAR(10));' | psql -h localhost -d testdb -U dbuser -W Password: CREATE TABLE > echo '\dt' | psql -h localhost -d testdb -U dbuser -W Password: List of relations Schema | Name | Type | Owner --------+---------+-------+-------- public | testtab | table | dbuser (1 row) > echo "INSERT INTO testtab (code) VALUES ('ABC');" | psql -h localhost -d testdb -U dbuser -W Password: INSERT 0 1 > echo "SELECT * FROM testtab;" | psql -h localhost -d testdb -U dbuser -W Password: id | code ----+------ 1 | ABC (1 row)