A Golang Rest API project with Postgres as the Database. Used Python
requests
to fetch the data from the API.
- You must have Golang installed in your system.
- You must also have Python installed in your system as well.
- Lastly, you will need to have PostgreSQL installed
- Fork this repo into your own Github
- Then clone the repo into your system
git clone url <directory>
- Run the following command on the terminal:
go mod init [module-name]
- Followed by this command which will install all the golang dependencies:
go mod tidy
- Create a
.env
file on the main folder and add the following code with your details:DB_HOST=127.0.0.1 DB_PORT=5432 DB_PASSWORD=[your password] DB_USER=[username] DB_NAME=[database name] DB_SSLMODE=disable
- Run the following command in your terminal:
python3 -m pip install requests
- Run the following commands on your postgres terminal/admin
CREATE TABLE users ( user_id numeric(10, 0) NOT NULL, name character varying(50) COLLATE pg_catalog."default" NOT NULL, age numeric(3, 0) NOT NULL, phone character varying(20) COLLATE pg_catalog."default", CONSTRAINT user_table_pkey PRIMARY KEY (user_id) ); INSERT INTO users (user_id, name, age, phone) VALUES (3, 'Jenny', 34, NULL); INSERT INTO users (user_id, name, age, phone) VALUES (2, 'Tom', 29, '1 800 123 1234'); INSERT INTO users (user_id, name, age, phone) VALUES (1, 'John', 28, NULL);
I am using vscode as my code editor. Watch to connect postgres to vscode
You need to open two terminals, one for the server and the other for the request.
Make sure you are on the main directory of the project
- Run the following command
cd frontend python3 main.py
- Then you shall see your data in a
json
format
Feel free to log any issues you have or become a contributer to the project