The Database Backend

Printer-friendly versionPDF version

The first thing that any webapp needs is a persistance storage. Generally speaking Oracle databases are generally a very safe choice. But for small and medium based businesses this can result in prohibitive costs. Mysql/MariaDB is a popular choice here and is running most of the web. But at this point I am deciding against the popular choice and going for Postgre. The main thinking and reason being that Postgre is considered very close to an Oracle DB. Hence later if I wish moving my schema over to an Oracle DB is generally considered do-able. Though I summarized my main reason, the choosing process took a bit longer. I took a look at arguments from both sides of Mysql/MariaDB and Postgre camps. But I'll leave that exercise for you.

So having chosen the database the next step was naturally to set up a development environment. So like I mentioned my development environment here, with Docker setting up the offficial Postgre instance was very simple. To begin with you need to seperate the data from the image. For that you create a volume with a one off command as below:

docker volume create WebShopPgData

The next step is simple as running the image using the volume above as below:

docker run --name WebShopPostgreDB -it --rm --publish 5432:5432 --volume pgdata:/var/lib/postgresql/data postgres:10

As soon as we stop the container, the image will get removed. For our next development session, you need to start the docker image again. The data that we use is going to remain persisted in the host volume that we created by the first command. So none of our data should get lost. A point to note here is that for production you most definitely will not start your database in the way above. This is purely for development purposes and when we start refinging our security, we will revisit this issue. Currently you started postgre sql without any password in trust mode whereby only localhost can connect to the database. If you image is running on some docker server or you have docker for windows installed, you might not be able to connect to it and should use the postgre documented environment variables "POSTGRES_USER" and "POSTGRES_PASSWORD".

I forgot the username and password in 2 days. So I decided to bake it into my own image based on the postgres official image. This makes it easy for me to develop and I also start cleanly with a DockerFile where I can reproducibly keep creating my environment. For that I created the "DockerFile" under a folder "db\docker\postgre" with the following contents:

FROM postgres:10 AS webshop-postgres-db
 
ENV POSTGRES_USER webshop
ENV POSTGRES_PASSWORD Welcome1
ENV POSTGRES_DB webshop
 
VOLUME /var/lib/postgresql/data

And you can than build the above docker file by changing into the "db\docker\postgre" folder and giving the following command:

docker build .

You can view your image than as follows after successful build

PS C:\Users\ahsan> docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
webshop-postgre-db         latest              5208d1f60542        5 days ago          228MB
postgres                   10                  084ec18124c8        5 weeks ago         228MB
docker4w/nsenter-dockerd   latest              cae870735e91        11 months ago       187kB

Now you can start the image as below and use the username password as you set from the env variables in docker file to connect.

docker run --name WebShopPostgreDB -it --rm --publish 5432:5432 --volume pgdata:/var/lib/postgresql/data webshop-postgre-db 

We already partially solved he next problem which is connecting to the database and managing the schema. For that purpose I initially have choosen pgadmin 4 currently. But I think my preference here might change over time. You should at this point create a new server connection on localhost at port 5432 and verify that your postgre instance is indeed running and we are up for business.

Next we are going to create our schema. For that you can go to the next article here.

Top level category:

Add new comment