In PostgreSQL 12 and later, it is possible to grant all privileges of a table in a database to a role/user/account.
The syntax is:
GRANT ALL ON table_name TO role_name;
If you want to grant it to all tables in the database then the syntax will be:
GRANT ALL ON ALL TABLES TO role_name;
If you want to grant it to all tables of a schema in the database then the syntax will be:
GRANT ALL ON ALL TABLES IN SCHEMA schema_name TO role_name;
Here's how I did it:
First, I logged into the Postgres database server:
psql -U postgres
Output
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1))
Type "help" for help.
Next, I created a database user/role/account:
CREATE ROLE alice4 WITH LOGIN PASSWORD 'securePass1';
Output
CREATE ROLE
Next, I created a table in the postgres database:
create table candidates (
candidate_id int generated always as identity,
first_name varchar(100) not null,
last_name varchar(100) not null,
email varchar(255) not null unique,
phone varchar(25) not null,
primary key(candidate_id)
);
Output
CREATE TABLE
Next, I granted all privileges on the candidates
table to the account/user/role alice4
GRANT ALL ON candidates TO alice4;
Output
GRANT
Next, logged out of the postgres database and the postgres
account/user/role:
exit
Next, I logged into the Postgres database using the alice4
account/user/role:
psql -U alice4 -W postgres
Output
Password:
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1))
Type "help" for help.
Next, I inserted data into the candidates
table that was previously created:
INSERT INTO candidates(first_name, last_name, email, phone)
VALUES('Joe','Com','joe.com@example.com','408-111-2222');
Output
INSERT 0 1
Next, I selected all data in the candidates
table that was previously created:
SELECT * FROM candidates;
Output
candidate_id | first_name | last_name | email | phone
--------------+------------+-----------+---------------------+--------------
1 | Joe | Com | joe.com@example.com | 408-111-2222
(1 row)
Resources: PostgreSQL GRANT
That's all
I hope this helps