By using AWS re:Post, you agree to the AWS re:Post Terms of Use

RDS Proxy doesn’t support command-line options error when accessing RDS from django

0

I have a Postgres RDS (engine version-16.1). i created a RDS Proxy for this RDS. when i try to access db from django's ORM. Application is throwing Below error ERROR - RDS Proxy doesn’t support command-line options

In Django settings database connects, we have defined below code for pointing to different schemas

        'OPTIONS': {
            'options': f"-c search_path={DATABASE_SCHEMA}"
        },

Now the Error is due to options parameter used in database settings of django Because RDS proxy doesn't support it. Is there a solution for this issue. please suggest

  • Please accept the answer if it was useful

asked 6 months ago396 views
1 Answer
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:

  1. 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.

  2. 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

  1. 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
        },
    }
}
  1. 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.
profile picture
EXPERT
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions