-
Notifications
You must be signed in to change notification settings - Fork 14
Docker container for a Postgres DB
authorjapps edited this page Dec 25, 2018
·
10 revisions
After following this page you should be successfully able to bring up a dockerised Postgres DB server and run some tests. (Authors: Nirmal Chandra)
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
$
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)
- 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

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