-
Notifications
You must be signed in to change notification settings - Fork 14
Docker container for a Postgres DB
After following the instructions from this page you should be able to successfully bring up a dockerized Postgres DB server and run some tests.
- How to configure/create a Docker compose file?
- How to bring down the container using the compose file ?
- How to bring up the container using the compose file ?
- How to check the status of PG DB and the Admin?
- How to verify the port has been exposed outside docker container ?
- Now, how to connect to the server using terminal command/console?
- Then, how to connect to a database and create a table e.g. employess?
- how to connect to the server using the Admin UI?
- Now, how to run the Zerocode tests?
- HelloWorld tests are here
- How to bring down the containers once you are done with your testing?
See here the docker-compose file
-
$ docker-compose -f pg_compose.yml down
If the containers are up, the above command will bring them down and if they are not up, then it does no harm.
-
Clone this repo
-
Go to the
zerocode-docker-factory/compose
dir in a terminal window -
Then run
-
$ docker-compose -f pg_compose.yml up -d
or
-
$ docker-compose -f pg_compose.yml up
-
This brings up both Postgres DB server and Postgres Admin server
$ docker ps
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8796ed628983 adminer "entrypoint.sh docke…" 25 seconds ago Up 23 seconds 0.0.0.0:8080->8080/tcp compose_adminer_1
09db1b3c982f postgres:9.3 "docker-entrypoint.s…" 25 seconds ago Up 23 seconds 0.0.0.0:35432->5432/tcp compose_db_1
$
Note-
See under the "NAMES" column(right hand ride) above, for `compose_db_1` as container name
and the "PORTS" column to see which port the container is listening to for the external connection.
Here the port is `35432` for external connections.
- To make sure Postgres is running and it has exposed the port to external connections, user command
-
nc -vz localhost
-
$ nc -vz localhost 35432
found 0 associations
found 1 connections:
1: flags=82<CONNECTED,PREFERRED>
outif lo0
src ::1 port 52494
dst ::1 port 35432
rank info not available
TCP aux info available
Connection to localhost port 35432 [tcp/*] succeeded!
Note- If a port is not exposed, you get the following message.
--------------------------------------------------------------
$ nc -vz localhost 35433
nc: connectx to localhost port 35433 (tcp) failed: Connection refused
nc: connectx to localhost port 35433 (tcp) failed: Connection refused
$
(Or go to Do via UI Click-Click section to create object via the admin UI)
root@94970ab3bcdc:/# su - postgres
postgres@94970ab3bcdc:~$ whoami
postgres
postgres@94970ab3bcdc:~$ psql
psql (9.3.25)
Type "help" for help.
postgres=#
postgres=# \list
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
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
(3 rows)
postgres=#
postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".
postgres=#
postgres=# \dt+
No relations found. <----- No table/objects exits at this points in the db
postgres=# CREATE TABLE employees ( id bigint NOT NULL, name character varying(10) NOT NULL);
CREATE TABLE
postgres=# \dt+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+-----------+-------+----------+---------+-------------
public | employees | table | postgres | 0 bytes |
(1 row)
postgres=#
postgres=# insert into employees(id, name) values(1, 'Jack');
INSERT 0 1
postgres=# insert into employees(id, name) values(2, 'Pulsar');
INSERT 0 1
postgres=# select * from employees;
id | name
----+--------
1 | Jack
2 | Pulsar
(2 rows)
postgres-# \q <------ Comes out from PSQL
postgres@94970ab3bcdc:~$ exit <------ Comes out from 'postgres' user
logout
root@94970ab3bcdc:/# exit <------- Comes out from docker container to local OS prompt
exit
$
Note : This is only a optional step i.e. another way of creating DB objects (i.e. without using the command line options explained above)
- Go to http://localhost:8080 in a browser
- Choose
Postgres SQL
from the dropdown - Enter user/password as
postgres/example
-> ClickLogin
button

- After logged in, Choose
postgres
from the list of DBs listed in the table

- Then create the
employees
table and insert some rows(copy the SQL queries from the command section above)- You can use various options in the admin panel including executing queries in an SQL window here.
- Clone the repo https://github.com/BeTheCodeWithYou/SpringBoot-ZeroCode-Integration
- Follow the Sample DB Executor Uncyclo on how to integrate in your project
In your test/resources/application_host.properties
upddate the host/port/db to below
## ---------------------------
## DB Host configs - Postgres
## ---------------------------
db_host_url=jdbc:postgresql://localhost:35432/postgres
db_username=postgres
db_password=example
and run the DB tests from here.
- See all related tests root folder GitHub link
- See GitHub repo for Zerocode Integration Test - Examples
Already explained in the above section, see here