Introduction
One of the important parts of a web app and backend API is the ability to store data in the database. Talking of databases, there are popularly of two categories:
Relational database like SQL
NoSQL database like MongoDB
As a newbie to backend development, I was presented with connecting a web app to a SQL database. In this blog post, I will discuss how I carried out the task, the challenges I faced during the task and the solution to them.
Reading The Friendly Manual
Anybody familiar with the Linux community would have heard the mantra RTFM (Read The Friendly Manual) two or more times. Working by this mantra, what came up to me before installing PostgreSQL on my machine was to consult the PostgreSQL official documentation for installation on a Linux.
As a Linux Mint user, I have to add the PostgreSQL repository to the Ubuntu package manager (and that's where the problem started). After running the following command as directed by the official documentation
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
then behold I got the following error:
The repository 'https://apt.postgresql.org/pub/repos/apt trusty-pgdg Release' does not have a Release file
But thanks to Stack Overflow, I was able to resolve the issue using the solution posted by a Stack Overflow user here.
Docker the Chad
A week later, I discussed the issue with a friend who informed me about Docker, for containerizing deployments.
Instead of going through the stress of installing a PostgreSQL executable on your machine, one can use the docker image of PostgreSQL.
To install docker, run the following:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
To verify the installation, run the following:
sudo docker run hello-world
Alternatively, you can install the docker desktop, which includes the docker CLI and the docker-compose. You can get the download here
Pulling Postgres container
Run the following to pull the Postgres container
docker pull postgres```the run```shdocker run --name my-postgres -e POSTGRES_PASSWORD=my_password -d -p 5432:5432 postgres
to start a Postgres instance, replace my-postgres
with any name you prefer and the password with the password you want to use for the Postgres user.
Voilà! You are ready to use the PostgreSQL database for your web apps.
HNG and the Way Forward
Two months ago, I had been writing Dart for Flutter apps, but I wanted to explore the backend side of the software development, so I started learning the Go programming language I love the constructs and the fact that it's very convenient to use for concurrent programs.
Well, this is my Zeroth log for the HNG internship and I look forward to learning, growing and enhancing my skills through the program.
HNG has groomed many frontiers of software engineering through their intensive 8-week internship. And there is an avenue for getting hired through the hiring process on their site, which is here.