Get a copy of the program:
git clone https://github.com/ivandi1980/Phonebook-Backend-Api.git
Go to the project directory:
cd Phonebook_Backend_REST-API_using_Golang_and_PostgreSQL
Build image and start the Backend app and PostgreSQL database:
docker-compose up -d --build --force-recreate
Check if they are running properly:
docker-compose ps
The output should be like this:
Name Command State Ports
----------------------------------------------------------------------------------
phonebook_backend_api ./Phonebook_Backend_REST Up 0.0.0.0:8000->8000/tcp,
-A ... :::8000->8000/tcp
phonebookdb docker-entrypoint.sh Up 0.0.0.0:5432->5432/tcp,
postgres :::5432->5432/tcp
Go inside of the container:
docker exec -it phonebookdb /bin/sh
Then connect to PostgreSQL database:
psql -U postgres
Now, add a table:
CREATE TABLE contacts (
id SERIAL,
PhoneNumber VARCHAR(250) NOT NULL,
FullName VARCHAR(250) NOT NULL,
Address VARCHAR(250) NOT NULL,
Email VARCHAR(250) NOT NULL,
PRIMARY KEY (id)
);
And add some data into the table:
INSERT INTO contacts (
PhoneNumber,
FullName,
Address,
Email
)
VALUES
('01234567890', 'Ivandjoh','Indonesia','ivan@ivan.com'),
('09876543210', 'Juna','Indonesia','juna@juna.com');
At the end, check if the data has been added successfully:
SELECT * FROM contacts;
The output should be like this:
id | phonenumber | fullname | address | email
----+----------------+--------------------+------------------------------------------------------------------------------+-----------------------
1 | 01234567890 | Ivandjoh | Indonesia| ivan@ivan.com
2 | 09876543210 | Juna | Indonesia | juna@juna.com
(2 rows)