reshma
reshma

Reputation: 722

Role creation with knexjs

I am new to PostgreSQL and node. I am using knex.js library. I need to create roles and assigned it to each table I created in PostgreSQL. I am not sure how can I achieve it. Is it to be done at the time of table migration? Or can I do it after migration? And if so how can I do it?

Upvotes: 6

Views: 2889

Answers (2)

Mikael Lepistö
Mikael Lepistö

Reputation: 19728

As others has mentioned, you need to use knex.raw and raw SQL statements (in this case postgresql flavor). Knex doesn't have any special APIs supporting setting roles.

Upvotes: 4

Vao Tsun
Vao Tsun

Reputation: 51649

I have no experience with knex, but I suppose you can use

knex.raw('create user blah password 'blah-blah') 

to create user. Of course your knex postgres user needs CREATE ROLE privs for it.

Assigning permissions on created tables to users might be done same way I suppose, eg:

knex.raw('grant select, update on table blah_blah to user blah') 

this should not require any additional permissions for knex db user, as It creates tables and thus is the owner.

Upvotes: 3

Related Questions