Database installation and configuration

After installing Docker on your machine, it will be time to run the containerized version of PostgreSQL. In order to do so, open a new Terminal window and launch the following command:

$ docker run --name postgres_springboot -e POSTGRES_PASSWORD=postgresPwd -e POSTGRES_DB=football_players_registry -d -p 5532:5432 postgres

This command triggers a pull from Docker's public registry for the PostgreSQL version labelled as the latest, downloading all of the layers that it needs to run the container on, as shown in the following code snippet:

Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
683abbb4ea60: Pull complete
c5856e38168a: Pull complete
c3e6f1ceebb0: Pull complete
3303bcd00128: Pull complete
ea95ff44bf6e: Pull complete
ea3f31f1e620: Pull complete
234873881fb2: Pull complete
f020aa822d21: Pull complete
27bad92d09a5: Pull complete
6849f0681f5a: Pull complete
a112faac8662: Pull complete
bc92d0ab9365: Pull complete
9e87959714b8: Pull complete
ac7c29b2bea7: Pull complete
Digest: sha256:d99f15cb8d0f47f0a66274afe30102b5bb7a95464d1e25acb66ccf7bd7bd8479
Status: Downloaded newer image for postgres:latest
83812c6e76656f6abab5bf1f00f07dca7105d5227df3b3b66382659fa55b5077

After that, the PostgreSQL image will be launched as a container. To verify this, you can launch the $ docker ps -a command, giving you a list of the created containers and their relative statuses:

CONTAINER ID IMAGE COMMAND CREATED
073daeefc52 postgres "docker-entrypoint.s..." Less than a second ago
STATUS PORTS NAMES
Up 4 seconds 0.0.0.0:5532->5432/tcp postgres_springboot

I had to split the command result into two lines, in order to make it readable.

You can also check the container logs, in order to retrieve information about the PostgreSQL status. Launch the following command:

$ docker logs -f 1073daeefc52

1073daeefc52 is the container ID. You should see the following information:

PostgreSQL init process complete; ready for start up.

2018-07-13 22:53:36.465 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
2018-07-13 22:53:36.466 UTC [1] LOG: listening on IPv6 address "::", port 5432
2018-07-13 22:53:36.469 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"

Now it's time to connect to the container, in order to manage it. Launch the following command:

$ docker exec -it 1073daeefc52 bash

1073daeefc52 is the container ID. Now log in to PostgreSQL with the following command:

$ psql -U postgres

Now you will be able to interact with the database server, as follows:

psql (10.4 (Debian 10.4-2.pgdg90+1))
Type "help" for help.
postgres=#

You should be able to see the football_players_registry database that we created when we created the container. Run the l command to verify the list of databases:

OK; it's time to create a simple table that will host the football players' data. Connect to the football_players database with the following command:

$ connect football_players_registry

Create the table with the following command:

CREATE TABLE FOOTBALL_PLAYER(
ID SERIAL PRIMARY KEY NOT NULL,
NAME VARCHAR(50) NOT NULL,
SURNAME VARCHAR(50) NOT NULL,
AGE INT NOT NULL,
TEAM VARCHAR(50) NOT NULL,

POSITION VARCHAR(50) NOT NULL,
PRICE NUMERIC
);

Check the table's structure with the following command:

$ d+ football_player

You should see the following result:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset