Setting up Postgres
The database we chose for the sample application is Postgres; we chose Postgres over other databases because of the wide variety of open source tools available for building, configuring, and maintaining Postgres. Postgres has been open source from version 1 since 1989 and it is used by big tech startups worldwide. The project has a lot of community support in terms of tools and utilities, which makes it easier to manage and maintain. The database is suitable for small all the way to big replicated data stores.
The easiest way to run it locally is to run it as a Docker container. First, use the following command to run Postgres:
docker run --name test-postgres \ -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
The command will run postgres
on port 5432
; if by any chance you have other applications or other Postgres instances listening to this port, the command will fail. If you need to run Postgres on a different port, change the -p
parameter (for example, -p 5555:5432
) to a different port number.
If successful, you will see the container ID printed out. The ID will differ from what is shown here:
f7bdfb7d2c10c5f0c9227c9b0a720f21d3c7fa65907eb0c546b8f20f12621102
Check whether Postgres is up and running by using docker ps
. The next thing to do is use the psql-client
tool to connect to Postgres to test it out. A list of the different Postgres client tools available on different platforms can be found here: https://wiki.postgresql.org/wiki/PostgreSQL_Clients.
We will use the standard postgres psql
tool using Docker. Open another terminal and use the following Docker command to run psql
:
docker exec -it test-postgres psql -h localhost -p 5432 -U postgres -d postgres
What we are doing is executing the psql
command inside the running Postgres container. You will see output such as the following, indicating that it has successfully connected to the Postgres database:
psql (12.3, server 14.5 (Debian 14.5-1.pgdg110+1)) WARNING: psql major version 12, server major version 14. Some psql features might not work. Type "help" for help. postgres=#
On a successful connection, you will see the following output. Note that the warning message mentions server major version 14 – this is to indicate that the server version is newer than the current psql
version as per the documentation (https://www.postgresql.org/docs/12/app-psql.html). The psql
client will work without any problem with the Postgres server:
psql (12.3, server 14.0 (Debian 14.0-1.pgdg110+1)) WARNING: psql major version 12, server major version 14. Some psql features might not work. Type "help" for help. postgres=#
Exit psql
to go back to the command prompt by typing exit
.
The following is some guidance on common errors when trying to connect to the database:
Error Message |
Description |
|
The password specified when running Postgres does not match with the password passed in using |
psql: error: could not connect to server: could not connect to server: Host is unreachable |
The IP address that you use to connect to Postgres is wrong. |
With this, you have completed the local setup of Postgres and are now ready to start looking into designing the database.