Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a remote postgres database in my django app?

What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'snackvoxadmin'
    },
    'users': {
        .....
    }


}

The database user has a url like similar to this one: postgres://a78adj1he81....

like image 236
Andie Rabino Avatar asked Sep 01 '15 08:09

Andie Rabino


2 Answers

You can decompose your database url and configure it like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

And the pattern for a database url is :

postgres://user:password@host:post/database

https://docs.djangoproject.com/en/1.8/ref/settings/#databases


Or you can use the package dj-database-url to directly use the database url.

E.g. from readme :

import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
like image 73
Lancelot HARDEL Avatar answered Nov 07 '22 09:11

Lancelot HARDEL


That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.

like image 1
Daniel Roseman Avatar answered Nov 07 '22 10:11

Daniel Roseman