Database installation and configuration

Having installed Docker on your machine, it's time to run the containerized version of PostgreSQL, as follows:

  1. Open a new Terminal window and launch the following command:
$ docker run --name postgres_thorntail 
-e POSTGRES_PASSWORD=postgresPwd -e POSTGRES_DB=football_players_registry
-d -p 5532:5432 postgres

This command will trigger a pull from Docker's public registry for the PostgreSQL version labeled as the latest, downloading all of the layers that it needs to run the container on, as depicted in the following output:

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 is launched as a container.

  1. To verify this, you can launch the $ docker ps -a command, which gives you a list of the containers created and their relative statuses:
CONTAINER ID  IMAGE    COMMAND                  CREATED        

1073daeefc52  postgres "docker-entrypoint.s..." Less than a second ago 

 

STATUS         PORTS                             NAMES 

Up 4 seconds   0.0.0.0:5532->5432/tcp     
postgres_thorntail

The command result has been split into two lines in order to make it readable.

  1. 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 get 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"
  1. 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  
  1. 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 launched the container's creation.

  1. Run the l command to verify the list of databases:
postgres=# l   

                                        List of databases   

          Name            |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges      

---------------------------+----------+----------+------------+------------+-----------------------   

 football_players_registry   | postgres | UTF8       | en_US.utf8 | en_US.utf8 |   

 postgres                 | postgres | UTF8    | en_US.utf8 | en_US.utf8 |   

 template0                | postgres | UTF8    | en_US.utf8 | en_US.utf8 | =c/postgres       +   

                          |             |             |             |             |   postgres=CTc/postgres   

 template1                | postgres | UTF8    | en_US.utf8 | en_US.utf8 | =c/postgres       +   

                          |             |             |             |             |   postgres=CTc/postgres   

(4 rows) 

OK. Now it's time to create a simple table that will host the football players' data.

  1. From the running Docker container instance, you should connect to the football_players database with the following command:
$ connect   football_players_registry  
  1. 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
);
  1. Check the table's structure with the following command:
$  d+   football_player   

You should see the following result:

football_players_registry=#   d+ football_player   

                           Table "public.football_player"   

  Column  | Type   | Collation | Nullable | Default | Storage  | Stats target | Description   

----------+-----------------------+-----------+----------+----------------------   

-----------------------+----------+--------------+-------------   

 id       |   integer |  | not null | nextval('football_player_id_seq'::regclass) | plain   |               |   

 name     |   character varying(50) | | not null | | extended |           |   

 surname  |   character varying(50) | | not null | | extended |           |   

 age      |   integer | | not null |   | plain | |   

 team     |   character varying(50) | | not null | | extended | |   

 position |   character varying(50) | | not null | | extended |    |   

 price    |   numeric | | | | main | |    

Indexes:   

   "football_player_pkey"   PRIMARY KEY, btree (id)   
..................Content has been hidden....................

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