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

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

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do