1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 23:28:02 +00:00

Add Check If User Role Exists For Database as a Postgres TIL

This commit is contained in:
jbranchaud
2022-11-03 19:57:02 -05:00
parent 29656fd7d6
commit 06b0db1209
2 changed files with 35 additions and 1 deletions

View File

@@ -0,0 +1,33 @@
# Check If User Role Exists For Database
User roles define who can access a database cluster and broadly what level of
control they have over that cluster.
The most straightforward way to check if a user role exists is to connect to
one of the databases in the cluster and run a query against the `pg_roles`
table.
```sql
select * from pg_roles where rolename='dev';
rolname
------------
dev
(1 row)
```
This same concept can be used in a script when automating some database setup.
To do that, we'll use `-c` (and some other flags) to dispatch a query to `psql`
from a shell context.
```bash
psql postgres -tXAc "SELECT 1 FROM pg_roles WHERE rolname='dev'" \
| grep -q 1 \
|| createuser --interactive dev
```
This queries for the value `1` if the user role named `dev` exists. The output
of that is piped to `grep` (in quiet mode, `-q`) to check if `1` is in the
output. If user roles doesn't exist and grep doesn't match on `1`, then the
right side of the _or_ (`||`) gets called. That command could be whatever. I've
chosen to call PostgreSQL's `createuser` to create the `dev` user role.