Go code for PostgreSQL. A Go language code which connects to PostgreSQL database for CRUD operation

GoWithPostgreSQL

GitHub go.mod Go version of a Go module Open in Visual Studio Code Ask Me Anything !


Go code for PostgreSQL. A Go language code which connects to PostgreSQL database for CRUD operation.

Softwares required

Go compiler --> https://go.dev/doc/install
Visual Studio code --> https://code.visualstudio.com/download
PostgreSQL --> https://www.postgresql.org/download/

Step 1

Open a command prompt and cd to your home directory.

On Linux or Mac:

cd

On Windows:

cd %HOMEPATH%

Step 2

Create a GoWithPostgreSQL directory. For example, from your home directory use the following commands:

mkdir GoWithPostgreSQL
cd GoWithPostgreSQL

Step 3

Start your module using the go mod init command.

go mod init gowithpostgresql

Step 4

In your text editor, create main.go and paste the content and save.

Step 5

Build the current module's packages and dependencies.

go mod tidy

Step 6

Prepare the database for the repo.

Step 6a

Create a database named customer

CREATE DATABASE customer
    WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    CONNECTION LIMIT = -1;

Step 6b

Create a schema named customer in customer database.

CREATE SCHEMA customer
    AUTHORIZATION postgres;

Step 6c

Create a table called customer.

CREATE TABLE IF NOT EXISTS customer.customer
(
    customerid bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ),
    firstname character varying(200) COLLATE pg_catalog."default",
    lastname character varying(200) COLLATE pg_catalog."default",
    CONSTRAINT customer_pkey PRIMARY KEY (customerid)
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS customer.customer
    OWNER to postgres;

Step 6d

Insert few reords to the customer table.

INSERT INTO customer.customer(
    firstname, lastname)
    VALUES  ('John', 'Doe'),
            ('Richard', 'Roe'),
            ('Mark', 'Moe');

Step 6e

Create the stored procedure insertcustomer

CREATE OR REPLACE PROCEDURE customer.insertcustomer(
    IN firstname character varying,
    IN lastname character varying)
LANGUAGE 'sql'
AS $BODY$
INSERT INTO customer.customer(
 firstname, lastname)
	VALUES (firstname,lastname);
$BODY$;

Step 7

Build the repo and execute the code in the terminal

go build .\main.go
.\main.exe

References

https://go.dev/blog/using-go-modules
https://go.dev/doc/tutorial/create-module
https://go.dev/doc/database/
https://github.com/lib/pq
https://www.postgresql.org/files/documentation/pdf/14/postgresql-14-A4.pdf

Similar Resources

ACID key-value database.

Coffer Simply ACID* key-value database. At the medium or even low latency it tries to provide greater throughput without losing the ACID properties of

Dec 7, 2022

A decentralized, trusted, high performance, SQL database with blockchain features

A decentralized, trusted, high performance, SQL database with blockchain features

中文简介 CovenantSQL(CQL) is a Byzantine Fault Tolerant relational database built on SQLite: ServerLess: Free, High Availabile, Auto Sync Database Service

Jan 3, 2023

Native GraphQL Database with graph backend

Native GraphQL Database with graph backend

The Only Native GraphQL Database With A Graph Backend. Dgraph is a horizontally scalable and distributed GraphQL database with a graph backend. It pro

Jan 4, 2023

EliasDB a graph-based database.

EliasDB a graph-based database.

EliasDB EliasDB is a graph-based database which aims to provide a lightweight solution for projects which want to store their data as a graph. Feature

Jan 4, 2023

LevelDB key/value database in Go.

This is an implementation of the LevelDB key/value database in the Go programming language. Installation go get github.com/syndtr/goleveldb/leveldb R

Jan 1, 2023

immudb - world’s fastest immutable database

immudb - world’s fastest immutable database

immudb Note: The master branch is the joint point for all ongoing development efforts. Thus it may be in an unstable state and should not be used in p

Jan 4, 2023

A high performance NoSQL Database Server powered by Go

A high performance NoSQL Database Server powered by Go

LedisDB Ledisdb is a high-performance NoSQL database library and server written in Go. It's similar to Redis but store data in disk. It supports many

Dec 26, 2022

Lightweight RESTful database engine based on stack data structures

Lightweight RESTful database engine based on stack data structures

piladb [pee-lah-dee-bee]. pila means stack or battery in Spanish. piladb is a lightweight RESTful database engine based on stack data structures. Crea

Nov 27, 2022

The Prometheus monitoring system and time series database.

Prometheus Visit prometheus.io for the full documentation, examples and guides. Prometheus, a Cloud Native Computing Foundation project, is a systems

Dec 31, 2022
LinDB is an open-source Time Series Database which provides high performance, high availability and horizontal scalability.
LinDB is an open-source Time Series Database which provides high performance, high availability and horizontal scalability.

LinDB is an open-source Time Series Database which provides high performance, high availability and horizontal scalability. LinDB stores all monitoring data of ELEME Inc, there is 88TB incremental writes per day and 2.7PB total raw data.

Jan 1, 2023
Owl is a db manager platform,committed to standardizing the data, index in the database and operations to the database, to avoid risks and failures.

Owl is a db manager platform,committed to standardizing the data, index in the database and operations to the database, to avoid risks and failures. capabilities which owl provides include Process approval、sql Audit、sql execute and execute as crontab、data backup and recover .

Nov 9, 2022
This is a simple graph database in SQLite, inspired by "SQLite as a document database".

About This is a simple graph database in SQLite, inspired by "SQLite as a document database". Structure The schema consists of just two structures: No

Jan 3, 2023
Hard Disk Database based on a former database

Hard Disk Database based on a former database

Nov 1, 2021
Simple key value database that use json files to store the database

KValDB Simple key value database that use json files to store the database, the key and the respective value. This simple database have two gRPC metho

Nov 13, 2021
a key-value store with multiple backends including leveldb, badgerdb, postgresql

Overview goukv is an abstraction layer for golang based key-value stores, it is easy to add any backend provider. Available Providers badgerdb: Badger

Jan 5, 2023
GoPostgreSQL - An example usage of PostgreSQL with GO, very simple since the objective is that others can read and learn
GoPostgreSQL - An example usage of PostgreSQL with GO, very simple since the objective is that others can read and learn

GoPostgreSQL - An example usage of PostgreSQL with GO, very simple since the objective is that others can read and learn

Feb 10, 2022
An embedded key/value database for Go.

bbolt bbolt is a fork of Ben Johnson's Bolt key/value store. The purpose of this fork is to provide the Go community with an active maintenance and de

Jan 1, 2023
BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support
BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support

BuntDB is a low-level, in-memory, key/value store in pure Go. It persists to disk, is ACID compliant, and uses locking for multiple readers and a sing

Dec 30, 2022
CockroachDB - the open source, cloud-native distributed SQL database.
CockroachDB - the open source, cloud-native distributed SQL database.

CockroachDB is a cloud-native SQL database for building global, scalable cloud services that survive disasters. What is CockroachDB? Docs Quickstart C

Jan 2, 2023