Setting up a Go web server with PostgreSQL and serving an API for CRUD operations involves several steps. Below is a guide tailored for Arch Linux.
Make sure you have the following installed:
- Go: Install it using
sudo pacman -S go. - PostgreSQL: Install it using
sudo pacman -S postgresql.
-
Initialize the PostgreSQL Database:
sudo -iu postgres initdb --locale en_US.UTF-8 -D /var/lib/postgres/data
-
Start and Enable the Service:
sudo systemctl start postgresql sudo systemctl enable postgresql -
Create a Database and User:
sudo -iu postgres psql
Inside the PostgreSQL shell:
CREATE DATABASE mydb; CREATE USER myuser WITH PASSWORD 'mypassword'; GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser; \q
-
Test Connection: Test with
psqlor a Go driver later.
-
Set Up Your Go Module:
mkdir Go_CRUD_server cd Go_CRUD_server go mod init Go_CRUD_server -
Install Required Libraries:
go get github.com/lib/pq go get github.com/gorilla/mux go get github.com/jmoiron/sqlx
-
Log into the database:
psql -U myuser -d mydb
-
Create the
posttable:CREATE SEQUENCE Posts_id_seq; CREATE TABLE public.posts ( id integer NOT NULL DEFAULT nextval('Posts_id_seq'::regclass), title text COLLATE pg_catalog."default", body text COLLATE pg_catalog."default", CONSTRAINT "Posts_pkey" PRIMARY KEY (id) ) WITH ( OIDS = FALSE ) TABLESPACE pg_default; ALTER TABLE public.posts OWNER to postgres; INSERT INTO posts(title, body) VALUES('the first title only','and this is the body of the first post.'); INSERT INTO posts(title, body) VALUES('yet another title','and some more copy text for another body.');
-
Run your server:
go run main.go
-
Test the API using tools like Postman or curl: ####Postman
hrrp://localhost:8000/posthrrp://localhost:8000/post/{id}####Curl- Create:
curl -X POST 127.0.0.1:8000/posts -H "Content-Type: application/json" -d '{"title": "A new post headline for a new post", "body": "but also a new body text here will appear"}' - Update:
curl -X PUT 127.0.0.1:8000/posts/3 -H "Content-Type: application/json" -d '{"title": "and this is the new title for post number 1","body": "and the brand new edited body text of post number 1"}' - Delete:
curl -X DELETE 127.0.0.1:8000/posts/4 -H "Accept: application/json"
- Use environment variables for sensitive data like credentials.
- Add validation for API inputs.
- Implement PUT for updating items.