1 Answer
- Newest
- Most votes
- Most comments
0
One way to achieve this is by using a custom database backend in Django to set the search path after establishing the connection. Here’s how you can do it:
-
Create a Custom Database Backend: Create a custom database backend that inherits from the default PostgreSQL backend and sets the search path after the connection is established.
-
Create the Custom Backend: Create a new Python module in your Django project (e.g., custom_db_backend). Inside this module, create a file base.py with the following content:
from django.db.backends.postgresql.base import DatabaseWrapper as PostgresDatabaseWrapper
class DatabaseWrapper(PostgresDatabaseWrapper):
def get_new_connection(self, conn_params):
connection = super().get_new_connection(conn_params)
search_path = conn_params.get('OPTIONS', {}).get('options', '')
if search_path:
with connection.cursor() as cursor:
cursor.execute(f"SET search_path TO {search_path.split('=')[-1]}")
return connection
- Update Django Settings: Update your Django DATABASES settings to use the custom database backend:
DATABASES = {
'default': {
'ENGINE': 'path.to.custom_db_backend', # Update this to the path of your custom backend
'NAME': 'your_db_name',
'USER': 'your_db_user',
'PASSWORD': 'your_db_password',
'HOST': 'your_db_host',
'PORT': 'your_db_port',
'OPTIONS': {
'options': f"-c search_path={DATABASE_SCHEMA}" # This will be processed by the custom backend
},
}
}
- Remove Unsupported Options: Since the options parameter is now processed by your custom backend, you can safely remove any unsupported options from the default connection parameters that would cause issues with RDS Proxy.
Relevant content
- asked 5 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 4 years ago
Please accept the answer if it was useful