{"id":1612,"date":"2020-09-16T10:58:21","date_gmt":"2020-09-16T08:58:21","guid":{"rendered":"http:\/\/www.netnea.com\/cms\/?p=1612"},"modified":"2020-09-16T13:10:08","modified_gmt":"2020-09-16T11:10:08","slug":"redundant-postgresql-connection-in-django","status":"publish","type":"post","link":"https:\/\/www.netnea.com\/cms\/2020\/09\/16\/redundant-postgresql-connection-in-django\/","title":{"rendered":"Redundant PostgreSQL connection in Django"},"content":{"rendered":"\n<p>Let&#8217;s assume you have a redundant cluster of PostgreSQL servers, eg built with <a href=\"https:\/\/repmgr.org\/\">repmgr<\/a>, and you want your Django application to benefit from this redundancy. As of Django 3.1, this is not really documented, but some easy steps gets you there, if you are using recent versions of PostgreSQL, psycopg2, Django and Python. First of all, you need to setup <a href=\"https:\/\/www.psycopg.org\/\">psycopg2<\/a> to access your database. A standard Django settings.py would look like this :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'yourdb,\n        'USER': 'dbuser',\n        'PASSWORD': 'dbpassword',\n        'HOST': 'dbserver1.example.com',\n        'PORT': '5432',\n    }\n}<\/code><\/pre>\n\n\n\n<p>Now let&#8217;s turn on some cool features like multiple entries in HOST and PORT, plus SSL certificate check :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'yourdb,\n        'USER': 'dbuser',\n        'PASSWORD': 'dbpassword',\n        'HOST': 'dbserver1.example.com,dbserver2.example.com',\n        'PORT': '5432,5432',\n        'OPTIONS' = {\n            'target_session_attrs': 'read-write',\n            'sslmode': 'verify-ca',\n            'sslrootcert': '\/data\/certs\/your_chain.pem'\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>You will not find these options in Django 3.1 documentation, but it seems to work fine, with these supplements passed down the chain to psycopg2 and the underlying <a href=\"https:\/\/www.postgresql.org\/docs\/current\/libpq-connect.html\">libpq<\/a>&#8216;s PQconnectdbParams.<\/p>\n\n\n\n<p>If your database access is read-only (unlikely in most Django applications), set target_session_attrs to &#8216;any&#8217;.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s assume you have a redundant cluster of PostgreSQL servers, eg built with repmgr, and you want your Django application to benefit from this redundancy. As of Django 3.1, this is not really documented, but some easy steps gets you there, if you are using recent versions of PostgreSQL, psycopg2, Django and Python. First of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[48,49],"class_list":{"0":"post-1612","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"tag-django","8":"tag-postgresql","9":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/comments?post=1612"}],"version-history":[{"count":2,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1612\/revisions"}],"predecessor-version":[{"id":1614,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/posts\/1612\/revisions\/1614"}],"wp:attachment":[{"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/media?parent=1612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/categories?post=1612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.netnea.com\/cms\/wp-json\/wp\/v2\/tags?post=1612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}