In this article I’m going to replace or default django SQLite database with a postures database. well start by doing this with a local postgres database, then showing you how we can use PostgreSQL on a production level application with AWS so we can use this database when our project is hosted on a live server.
- Download and install PostgreSQL & PG Admin
- Login to PG admin & Create Database
- Connect database to Django App & run migrations
- Create database on AWS
- Connect to live AWS Database with PG admin & Django
(1) Dowoad and install PostgreSQL & PG Admin
If you haven’t already you will need to install postgres onto our computer and PG admin to interact with our databse. PG Admin is an interface we can use to interact with our postgres database so it will be much easier to use instead of the command line.
You can install postgres by postgresql.org and downloading the postgres installer. I am using windows so I will chose the latest version of postgres with the windows option. Choose what is right for your computer type.
Once the installer is downloaded go ahead and follow the steps to get you basic setup complete.
Install PG Admin
PG Admin provides us a great insterface to interact with out postgres database. You can download it by going to pgadmin.org/download. From here select the version compatible with your computer & select the latest version of PG Admin.
(2) Login to PG admin & Create Database
Once PostgreSQL & PG Admin are installed, open PG Admin and create a brand new database by creating a new server group & a new server inside the new server group. (Server Group –> Server)
For out connection (Step 3) we will set the “Host name/address” to localhost & the password will be whichever password you set during the installation of PG Admin.
No Inside out servergoup we can create a database by right clicking on “databases” and selecting “create –> Database”
From here just set the database name and connect a user. I’ll leave my to default to my postgres user.
Once the database is created it will now appear in the list of databases. We can now connect this database to our django app
(3) Connect database to Django App & run migrations
In you django apps setttings.py file commnet out the default database connection and add in a new one like in the image below.
We updated the following attributes:
- ENGINE: “django.db.backends.postgresql”
- NAME: ‘Our new database name we created in pg admin’
- USER: PG Admin/Postgres user
- PASSWORD: Password we set on local installation
- HOST: localhost
- PORT 5432
Once we set this we can run “python manage.py migrate” if we already made our migrations earlier. This will create all our database tables in pg admin under out database –> schemas –> Public –> Tables. You can view each tables data by right clicking on the table and selecting “view/edit data”
(4) Create database on AWS
No we want to create a postgres databases somewhere where we can connect on a live project and use whenever we push out project to a server. For now what we will do is create a postgres database on AWS & use a live database with a local project.
So if you dont already go to aws.amazon.com and create an account. You will need to provide your card information but don’t worry because we will work within the free tier so you wont get charged until you upgrade.
Once you set up you AWS account do a search for RDS (Relational Database Service) inside your AWS management console.
Select “Create DB”
Form here we will stick to most of the default settings. Once we select postgres as our DB, we will select the free tier version to get us started for this demo.
You good leave your username as the default postgres but I will give mine a custom name and set my password.
Now what we want to do before I create this database is to set ” Publicly accessible” to yes. This is a very important step if we want to to connect with our database outside of AWS with programs like PG Admin. Do this by expanding the “Additional connectivity configuration” and selecting yes. This can later be updated if you miss this step from your DB instance inside the “Modify” tab.
Finally under Additional configuration we want to create our database name.
No go ahead and hit “Create DB” at the bottom of the page and give AWS a few minutes to create your database.
(5) Connect to live AWS Database with PG admin & Django
Last step in this process is to connect our live Postgres database hosted in AWS to PG admin so we can view our tables and to our django project.
Once our new database is ready we will use our “Endpoint” and “Username/password” we set to connect with PG Admin. An endpoint is the url that points to our db instance and we can get it by opening up our DB instance in AWS and finding it inside the “Connectivity & security” tab in our configurations page.
In PG Admin, create a new server connection and inside the “Connection” tab we will set our “Host name/Address” to our database endpoint instead of “localhost” like we did earlier.
Username and password will be set to whatever we created in AWS when we made this Database.
Once this is done you should see you new database appear. Now all we need to do is update our connection in our django app and run the migration again to create our tables.
In your settings.py file inside the DATABSES configuration update the following:
- ENGINE: “django.db.backends.postgresql”
- NAME: ‘Our new database name we created in AWS’
- USER: AWS USER
- PASSWORD: AWS DB PASSWORD
- HOST: Database endpoint url
- PORT 5432