Skip to main content

Django PostgreSQL Installation and Setup

We’ll know that SQLite is very powerful, embedded relational database management system and it offers a really amazing set of tools to manage approximately all sorts of data. But when it comes Multi-user applications (where multiple users want to use the same databases),  It fails. So It is recommended to choose a fully featured relational database management system rather than SQLite.

Some examples of Fully featured RDBMS are MySQL, PostgreSQL, Oracle databases.

Django is very flexible in terms of changing databases.

So in this article, we’ll see how we can change the database from SQLite to PostgreSQL.

Prerequisites

Instead of creating everything from scratch, we’re taking the example of our previous article, in which we’re using SQLite database.

If you haven’t read our previous article yet, then here it is https://www.thecrazyprogrammer.com/2019/01/django-models.html

Let’s start.

Step 1: Install PostgreSQL

To Install PostgreSQL for Django in Ubuntu:

Open terminal and type these two commands.

sudo apt-get update

sudo apt-get install python-pip python-dev libpq-dev postgresql       postgresql-         contrib

To Install PostgreSQL for Django in Windows:

Download the installer from its official site: https://www.postgresql.org/download/windows/

And follow the instructions mentioned in the link. While installing you have to enter a password and a port number. Note it down.

To Install PostgreSQL for Django in Mac OS:

Like windows, you need to download the installer from its official site: https://www.postgresql.org/download/macosx/

While installing you have to enter a password and a port number. Note it down.

Step 2: Setup User and Database

Login using your password.

Here I am using a Ubuntu 18.04 and I am accessing the PostgreSQL from the terminal, So I have to switch to the user postgres that was created while installing PostgreSQL.

To switch the user to postgres, open terminal and type

sudo su – postgres

Django PostgreSQL 1

Now you’re ready to enter the shell session for the postgres user. Log into a Postgres session by typing:

psqlDjango PostgreSQL 2

Now create a database by typing:

CREATE DATABASE myproject;

Django PostgreSQL 3

Choose the database name that is more relevant to your project. As it is just an example so I am using here myproject.

Now create a user to use the database that we’ve just created. To create a user type:

CREATE USER myuser WITH PASSWORD ‘mypassword’;

Django PostgreSQL 4

So our user and database are created.

Now, at last, give the rights to the user to access the database type:

GRANT ALL PRIVILEGES ON DATABASE myproject TO myuser;

Django PostgreSQL 5

Now just type \q to get back to the postgres user’s shell session then type exit.

Django PostgreSQL 6

That’s all about database setup.

Step 4 : Install psycopg2

psycopg2 is a most popular PostgreSQL database adapter to work with Python. To install pycopg2 type:

pip install django psycopg2

Django PostgreSQL 7

Note: I am using pip3 instead of pip because I have both the versions installed in my Ubuntu Linux.

 Step 5: Edit Your Project’s Settings File

Open your project’s setting file in write mode and go to the DATABASES section.

Django PostgreSQL 8

Here you can see that sqlite3 is connected with our project. To change it to PostgreSQL just change these lines as:

DATABASES = {

    ‘default’: {

        ‘ENGINE’: ‘django.db.backends.postgresql’,

        ‘NAME’: ‘myproject’,

        ‘USER’: ‘myuser,

        ‘PASSWORD’ : ‘mypassword’,

        ‘HOST’ : ‘localhost’,

        ‘PORT’ : ‘5432’

    }

}

Django PostgreSQL 9

In the above code, NAME, USER, PASSWORD are name of database, name of your user and password that we’ve created while creating the user.

And PORT is same number that I recommended to note down while installing the PostgreSQL.

Step 6: Migrate Your Project

So we’ve installed PostgreSQL and configured the settings.py file. Now last step is to migrate your project.

Open your project directory in terminal and type:

python manage.py runserver

Django PostgreSQL 10

All set, test and run your project.

That’s all about changing database from SQLite to PostgreSQL.

Note: While migrating, if you’re facing a warning like:

UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use “pip install psycopg2-binary” instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.

  “””)

You can solve this issue with installing psycopg2-binary  by using following command:

pip install psycopg2-binary

If you’ve any query related to Django PostgreSQL installation and setup, please let us know in the comment box.

The post Django PostgreSQL Installation and Setup appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2019/01/django-postgresql-installation-and-setup.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold ...

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and...