11

I'm learning python and I want to understand the database section and when setting up for postgresql database.

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

Are all the values necessary?

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

Specifically USER, PASSWORD, HOST, PORT? Is USER and PASSWORD values that we can create in django settings.py? Or is this the actual USER/PASSWORD of the database? Also, HOST is currently 127.0.0.1 for localhost, but when deploying to production, do I change this to the domain name (http://www.example.com)? And PORT, is it necessary?

4
  • 1
    Not an answer but I like the one liner way.. DATABASES['default'] = dj_database_url.config(default="postgres://USER:PASSWORD@HOST:PORT/NAME") Commented Jul 29, 2016 at 18:50
  • @RajaSimon seems a bit unreadable and would violate PEP-8 line length conventions, if you're adhering to that. Commented Jul 30, 2016 at 16:59
  • Compare to that dict this one liner is best... Commented Jul 30, 2016 at 18:11
  • You could also use djangosecure to store your settings. stackoverflow.com/questions/24492956/… Commented May 29, 2017 at 5:56

3 Answers 3

14

Yes! all of those information are necessary , there is no way you could connect to the database unless those values are specified.

Yes! user and password are the actual credentials of your PostgreSQL database.

regarding deployment, you should set the correct IP/host where your production database is located. that might be example.com or xxx.xxx.xxx.xxx

I think you are concerned about security (revealing your database credentials in the source) if that is the case you could put your credentials in secure config file like .env and use this library to work with your config file.

Sign up to request clarification or add additional context in comments.

3 Comments

for username and password, is that just something we come up with ourselves or should this match some kind of username and password that I already (or should) have with postgresql?
I don't think I've ever setup my own username and password for PostgreSQL, I have PostgreSQL app on my Mac, and when I launch, it just launches, so how do I know what my actual credentials are? @Que
well first you need to create a database with you database administration tool (I think that is pgAdmin) when you create a database with pgAdmin you will be promoted to enter password. that is your password. if you have trouble doing that. check this [ youtube.com/watch?v=ghTksCsFBcI ] YouTube tutorial , the guy shows how to create a database at the end of the video.
2

Specifically USER, PASSWORD, HOST, PORT? Is USER and PASSWORD values that we can create in django settings.py? Or is this the actual USER/PASSWORD of the database? Also, HOST is currently 127.0.0.1 for localhost, but when deploying to production, do I change this to the domain name (http://www.example.com)? And PORT, is it necessary?

The USER and PASSWORD is what you configure in the database, then you enter it in the file.

The HOST is the IP address or hostname where the server is running. In production, you have to check with your hosting provider for the correct details; it is rare that it is your domain name.

The PORT you only need to adjust if its different than the default port (5432). If it is different, your host will tell you.

Finally, keep in mind that http://www.example.com is not a domain name, this the the complete URL. The domain name is example.com, and the host is www, the fully qualified hostname is www.example.com.

1 Comment

For mac postgres app, by default, I don't need to do a username and password if my environment is in development and i'm working in root (main account) right? When I was working on Ruby on Rails, I never set a username and password in my database for postgresql.
0

Yes. See here for Django 2.1 or other versions. You could do the following:

  1. Login to Heroku
  2. Go to Resources
  3. Search for Postgres (Free version/Hobby)
  4. Then click on the newly generated database link
  5. Go to settings Under Admin, go to Credentials

Then you can add these variables in your Django production settings, preferably kept secret and called as environment variables () or you can define in the Heroku CLI (using the SET command in Windows). More explanation here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.