django python postgres

Django application with postgreSQL database

In this tutorial we will see how to create and configure a Django application https://www.djangoproject.com/ using the free PostgreSQL database offered by https://neon.tech/ in its Free tier.

To do this we will need to have Python installed (it is usually installed) and the libpq-dev library in our operating system. In Ubuntu / Debian and derived distributions the library is installed using the command:

sudo apt-get install libpq-dev

We will work inside a virtual venv environment offered by Python from its version 3.3 https://docs.python.org/es/3/library/venv.html. This allows us to isolate our application with all its dependencies avoiding installing libraries (in this case the Django framework) in our operating system.

Another advantage is that we will be able to migrate our application to another environment, for example to the cloud, and it will work in a similar way as it does in our local environment.

Neon Postgres Serverless

Neon(https://neon.tech/) is a Postgres Serverless cloud service that splits storage and compute to offer automatic scaling, branching (branches for development) and unlimited storage.

In https://console.neon.tech/sign_in you can create a free account that will give you a PostgreSQL database with 30 GB of space, 10branches for development and 3 vCPUs (virtual CPUs).

Once the account is created, you will have access to the Neon panel from where you will be able to create the free database. To do this, create a project(Neon project) by clicking on New Project and specifying a name, the PostgreSQL version and a region or zone. Once created, copy and/or download the connection data that will later be needed to configure Django.

Django to create web applications

Django is a high-level framework for Python that allows you to quickly develop web applications using a pragmatic and clean design. It takes care of the basic problems of web development so we can focus on the specific functionalities we need and avoid having to rewrite code.

Django is free,open source and has an active community that maintains https://www.djangoproject.com/community/ and has produced considerable documentation over its more than 15 years of participation.

Create a web application

The steps to follow to create the web application using the Django framework and Python are the following:

  • We create a folder on our hard drive and enter it to save the application files:
mkdir postgresql-app
cd postgresql-app/

OPTIONAL: If we have Docker installed we could launch a container with posgres inside and test our connection to Neon from there:

docker run -it -u 0 --name postgresql-app postgres /bin/bash
psql -h pg.neon.tech
  • The next step is to create a virtual Python environment in order to install Django:
# Creamos el ambiente virtual  
python -m venv venv
# Lo activamos
source venv/bin/activate
# Instalamos Django
pip install django
# Verificamos que Django está correctamente instalado
python3 -c "import django; print(django.get_version())"
  • Now it's time to create the web application:
python3 venv/bin/django-admin startproject postgresql_app
# Configuramos la aplicación 
vim postgresql_app/postgresql_app/settings.py 

The example for the correct configuration of the DATABASES section of this file can be found at https://neon.tech/docs/guides/django#configure-django-connection-settings. We must change 'database_name', 'username', 'password' and 'database_server' with our data and assign port 5432 to 'PORT', which is the default port.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'nombre_base_datos',
        'USER': 'nombre_usuario',
        'PASSWORD': 'password',
        'HOST': 'servidor_base_datos',
        'PORT': '5432',
    }
}
  • Now we go into the postgresql_app directory and run the manage.py file:
cd postgresql_app/
python3 manage.py migrate
  • Finally we run Django. In my case I have set it to run on port 7000. If not specified, the default port is 8000:

python3 manage.py runserver 7000

Cover image: Image presented in a standard Django installation.

If you are looking for a trainer to run this course or another training activity (webinar, workshops, bootcamps, etc.) in your organisation, you can find me through the contact page. Thank you very much.

If you liked the article you can help me by donating with cryptocurrencies. Thank you!!!