Run Program

Get a copy of the program:

  git clone https://github.com/hmdhszd/Phonebook_Backend_REST-API_using_Golang_and_PostgreSQL.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  

Connect to database and add a table

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
    ('00989121234567', 'Hamid Hosseinzadeh','No.59, Iran, Tehran','[email protected]'),
    ('0011234567890', 'Brad Pit','No.24, Iran, Yazd','[email protected]'),
    ('0010982847492', 'George Clooney','No.100, USA, LA','[email protected]'),
    ('0029848289238', 'Leonardo DiCaprio','9255 Sunset Blvd., Suite 615, West Hollywood, California 90069 United States','[email protected]');

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 | 00989121234567 | Hamid Hosseinzadeh | No.59, Iran, Tehran                                                          | [email protected]
  2 | 0011234567890  | Brad Pit           | No.24, Iran, Yazd                                                            | [email protected]
  3 | 0010982847492  | George Clooney     | No.100, USA, LA                                                              | [email protected]
  4 | 0029848289238  | Leonardo DiCaprio  | 9255 Sunset Blvd., Suite 615, West Hollywood, California 90069 United States | [email protected]
(4 rows)

Now, we can test our API using Postman:

Add a new contact

App Screenshot

Get all contacts

App Screenshot

Search in contacts

App Screenshot

Delete a contact by it’s name

App Screenshot

Delete a contact by it’s number

App Screenshot

Delete all contacts

App Screenshot

GitHub

View Github