-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcreate_read_only_user.py
88 lines (76 loc) · 2.66 KB
/
create_read_only_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import psycopg2
from psycopg2 import sql
class DatabaseUserCreator:
def __init__(self, host, port, dbname, user, password):
self.conn_string = (
f"host={host} port={port} dbname={dbname} user={user} password={password}"
)
self.conn = None
self.cursor = None
def connect(self):
self.conn = psycopg2.connect(self.conn_string)
self.cursor = self.conn.cursor()
def close(self):
if self.cursor is not None:
self.cursor.close()
if self.conn is not None:
self.conn.close()
def create_read_only_user(self, new_user, new_user_password):
self.connect()
try:
self.cursor.execute(
sql.SQL("CREATE USER {} WITH PASSWORD %s").format(
sql.Identifier(new_user)
),
[new_user_password],
)
self.cursor.execute(
sql.SQL("GRANT CONNECT ON DATABASE {} TO {}").format(
sql.Identifier(self.conn.info.dbname),
sql.Identifier(new_user),
)
)
self.cursor.execute(
sql.SQL("GRANT SELECT ON ALL TABLES IN SCHEMA public TO {}").format(
sql.Identifier(new_user)
)
)
self.cursor.execute(
sql.SQL(
"ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO {}"
).format(sql.Identifier(new_user))
)
self.conn.commit()
print(f"Read-only user {new_user} created successfully.")
except Exception as e:
self.conn.rollback()
print(f"Error creating read-only user: {e}")
finally:
self.close()
def list_users(self):
self.connect()
try:
self.cursor.execute(sql.SQL("SELECT usename FROM pg_user"))
users = self.cursor.fetchall()
return users
finally:
self.close()
def list_roles(self):
self.connect()
try:
self.cursor.execute(
sql.SQL(
"SELECT rolname AS role_name, rolsuper AS is_superuser FROM pg_roles"
)
)
roles = self.cursor.fetchall()
return roles
finally:
self.close()
if __name__ == "__main__":
creator = DatabaseUserCreator("localhost", "5432", "vectordb", "admin", "admin")
creator.create_read_only_user("readonlyuser", "readonlypassword")
users = creator.list_users()
print("Users:", users)
roles = creator.list_roles()
print("Roles:", roles)