Search code examples
djangodjango-settings

Django Connect to remote database using ssh and pem file


I can access database by SSH to some aws instance using pem file.

How do i connect the django to that postgres database.

Django Database connect has sslkey etc in options. But couldn't find the ssh tunnel option.

Any pointers pls

Update:- Due to Company policies can’t change the settings like white list up etc . In aws cloud no issues as all are white listed . I wanted to connect local Django setup to staging / uat database for testing


Solution

  • There are a few options

    You can either create an ssh tunnel to map the remote server to a local port

    ssh -L <local port>:127.0.0.1:<remote port> <username>@<remote host> -N
    

    Whiles this process is active the local port will connect to the remote port on the remote host. You can something like supervisord plus a script to make sure the ssh connection launches before your app.

    Another option would be django-db-tunnel