Django Blog #32: Installing PostgreSQL

by Alex
Django Blog #32: Installing PostgreSQL

The project now uses SQLite. This is necessary for the development process. But the deployment will require a more powerful database: PostgreSQL, MySQL or Oracle. Let’s choose the former to get its full-text search features. If you are using Linux, install the components needed to run PostgreSQL as follows:

sudo apt-get install libpq-dev python-dev

Then install the database itself:

sudo apt-get install postgresql postgresql-contrib

If you have macOS or Windows, download PostgreSQL from https://www.postgresql.org/download/ and install it. You will also need a PostgreSQL adapter called Psycopg2 for Python. This command will install it:

pip install psycopg2==2.7.7

Create a PostgreSQL database. Open the console and type the following commands:

su postgres 
createuser -dP blog

Next, you will need to enter a password for the new user. Enter it and create a blog database, making it the owner of the user that was just created with the command:

createdb -E utf8 -U blog blog

Edit the settings.py file and change the DATABASES setting to look like this:

DATABASES = { 
    'default': { 
        'ENGINE': 'django.db.backends.postgresql',  
        'NAME': 'blog',  
        'USER': 'blog',  
        'PASSWORD': '*****', # password  
    } 
}

Replace the data above with the name of the new database and the data of the new user. The new database is empty. Run the following command to apply all database migrations:

python manage.py migrate

Finally, create a new superuser:

python manage.py createsuperuser  

You can start the development server and go to the administrative site https://127.0.0.1:8000/admin/ with the new data. Since the database has changed, there are no posts here. Fill it out with the base posts so that it is searchable.

Related Posts

LEAVE A COMMENT