Postgres is a popular relational database that you can use in your Python/Django project. Setting it up however, can be a little overwhelming.
Graciously, with Django models, you don’t have to write too many SQL queries to create your database, tables and even add data on your psql shell.
Here is your step by step summary to install and set up PostgreSQL on Ubuntu 22.04:
Check whether Postgres is installed
- Open a terminal or command prompt.
- Run the following command:
psql --version
- The version information will be displayed in the output.
Install Postgres
sudo apt update
sudo apt install postgresql postgresql-contrib
Start the PostgreSQL service
sudo systemctl start postgresql.service
Switch over to the postgres account
sudo -i -u postgres
Access the psql shell
psql
Create a superuser role with login privileges, and password
CREATE ROLE <name_of_role> LOGIN PASSWORD <password_as_a string> BYPASSRLS;
Create database
CREATE DATABASE <database_name>;
Grant privileges on the database
GRANT ALL PRIVILEGES ON DATABASE <database_name> TO <created_superuser>;
Connect to the database (outside of psql and user postgres)
psql -U payments_admin -h localhost -p 5433 -d payments;
That's it, you are now ready to start going migrations to your database, and create tables based on your Django models.
Top comments (0)