Skip to content

Commit 33cc571

Browse files
committed
Enhance PostgresManager to grant schema permissions on database creation
- Close initial connection after user and database creation - Connect to the newly created database to grant schema permissions - Implement schema permission grants for public schema, including ownership and default privileges
1 parent fc072ef commit 33cc571

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

app/db/postgres.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@ async def create_database(self, owner: str, name: str = None, user_id: int = Non
4848
await conn.execute(f'CREATE USER {db_user} WITH PASSWORD \'{db_password}\'')
4949
await conn.execute(f'GRANT ALL PRIVILEGES ON DATABASE {db_name} TO {db_user}')
5050

51+
# Close the initial connection
52+
await conn.close()
53+
54+
# Connect to the newly created database to grant schema permissions
55+
conn = await asyncpg.connect(
56+
host=self.host,
57+
port=self.port,
58+
user=self.admin_user,
59+
password=self.admin_password,
60+
database=db_name
61+
)
62+
63+
try:
64+
# Grant schema permissions
65+
await conn.execute(f'GRANT ALL ON SCHEMA public TO {db_user}')
66+
await conn.execute(f'ALTER SCHEMA public OWNER TO {db_user}')
67+
await conn.execute(f'ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO {db_user}')
68+
await conn.execute(f'ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO {db_user}')
69+
await conn.execute(f'ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO {db_user}')
70+
finally:
71+
await conn.close()
72+
5173
return {
5274
"database_name": db_name,
5375
"database_username": db_user,
@@ -59,8 +81,6 @@ async def create_database(self, owner: str, name: str = None, user_id: int = Non
5981
except Exception as e:
6082
print(f"Error creating database: {str(e)}")
6183
raise
62-
finally:
63-
await conn.close()
6484

6585
async def delete_database(self, database_name: str, litellm_token: str = None):
6686
conn = await asyncpg.connect(

0 commit comments

Comments
 (0)