6

In Django 1.9, what changes do I have to make in settings.py in order to connect to a postgresql database using cert authentication?

2 Answers 2

5

Adding this to settings.py worked for me:

import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '<name>',
        'USER': '<user>',
        'PASSWORD': '<password>',
        'HOST': '<host>',
        'PORT': '25060',   
        'OPTIONS': {
            'sslmode': 'verify-full',
            'sslrootcert': os.path.join(BASE_DIR, 'ca-certificate.crt'),
        },
    },
}
Sign up to request clarification or add additional context in comments.

5 Comments

is it okay to place the certificate file in repo?
@MalikFaiq it's not.
@MalikFaiq The certificate file is public by definition, so there would be no security vulnerability posed by storing it in the repo and in some cases it may make sense to do so, however that is likely not best from a configuration management perspective.
This answer describes using certificates to make an SSL secured connection to postgresql using password authentication. I believe the OP was asking about "Certificate Authentication" (see jjanes answer)
@AllAboutMike, you are correct, not sure how I missed something so obvious now that I re-read it.
4

To my knowledge, django uses psycopg2, which in turn uses libpq. This means you shouldn't need to make any changes to settings.py.

You have to configure the server properly so it asks for and knows how to validate the certificate, and on the client side you need to put the crt and the key in the correct directory (e.g. ~/.postgresql) so that libpq knows how to find them. Once that is done properly, it should just work with no changes to any Django-specific code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.