Skip to content

Commit

Permalink
Added simple example on golang that using pgx for connect to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Nov 2, 2024
1 parent 15ce9d4 commit 020b3b7
Show file tree
Hide file tree
Showing 334 changed files with 169,848 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/golang/cource/06g_databases/db_types.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Relational DataBase Management System (RDBMS): PostgreSQL, MySQL, Oracle
Document-oriented NoSQL database: MongoDB, CouchDB
Key-Value store: Redis, etcd, memcached
Wide database:
Column-oriented database management system: Clickhouse
Row-oriented storage: Cassandra
Search database: Solr, Splunk, ElasticSearch
Graph Database management: Neo4j, Virtuoso, OrientDB
Time seried database: TimescaleDB, Influx, Prometheus
Etc: GeoMesa, QLDB, SpaceTime, SparkleDB
25 changes: 25 additions & 0 deletions examples/golang/cource/06g_databases/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: '3.1'

services:
sql-db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: some
ports:
- "5432:5432"

key-db:
image: redis
restart: always
ports:
- "6379:6379"

no-sql:
image: mongo
restart: always
environment:
MONGO_INITDB_DATABASE: master
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: some

21 changes: 21 additions & 0 deletions examples/golang/cource/06g_databases/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module 06g_databases

go 1.23.2

require (
github.com/georgysavva/scany v1.2.2
github.com/jackc/pgx/v4 v4.18.3
)

require (
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
golang.org/x/crypto v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
214 changes: 214 additions & 0 deletions examples/golang/cource/06g_databases/go.sum

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions examples/golang/cource/06g_databases/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main
// GORM
// https://github.com/jackc/pgx

import (
"fmt"
"context"
"os"
"github.com/jackc/pgx/v4"
// "github.com/jackc/pgx/v4/pgxpool"
// "github.com/georgysavva/scany/pgxscan"
)

func PostgresSimpleQuery() (name string, position string) {
var id int64
ctx := context.Background()

conn, err := pgx.Connect(ctx, "postgres://postgres:some@localhost:5432/postgres")
if err != nil {
fmt.Fprintf(os.Stderr, "Can't connect to db: %v\n", err)
os.Exit(1)
}
defer conn.Close(ctx)

err = conn.QueryRow(ctx, "select * from users where id=$1", 2).Scan(&id, &name, &position)
if err != nil {
fmt.Fprintf(os.Stderr, "Query faild: %v\n", err)
os.Exit(1)
}
return name, position
}

func main() {
fmt.Println(PostgresSimpleQuery())
}
49 changes: 49 additions & 0 deletions examples/golang/cource/06g_databases/postgres.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
rank VARCHAR(255)
);

CREATE UNIQUE INDEX users_id_uindex ON cars(id);

CREATE TABLE cars (
id SERIAL PRIMARY KEY,
user_id INT,
colour VARCHAR(255),
brand VARCHAR(255),
license_plate VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE UNIQUE INDEX cars_id_uindex ON cars(id);

INSERT INTO users (name, rank) VALUES
('Alice Johnson', 'Manager'),
('Bob Smith', 'Developer'),
('Charlie Brown', 'Designer'),
('Diana Prince', 'Marketing Specialist'),
('Ethan Hunt', 'Sales Representative'),
('Fiona Apple', 'HR Specialist'),
('George Lucas', 'Project Manager'),
('Hannah Montana', 'Data Analyst'),
('Ian Malcolm', 'Customer Support'),
('Jane Doe', 'Finance Officer');

INSERT INTO cars (user_id, colour, brand, license_plate) VALUES
(1, 'Red', 'Toyota', 'A1B2C3'),
(1, 'Yellow', 'Mercedes', 'S9T0U1'),
(2, 'Blue', 'Honda', 'D4E5F6'),
(2, 'Purple', 'Audi', 'V2W3X4'),
(3, 'Green', 'Ford', 'G7H8I9'),
(3, 'Orange', 'Volkswagen', 'Y5Z6A7'),
(4, 'Black', 'Chevrolet', 'J0K1L2'),
(4, 'Pink', 'Hyundai', 'B8C9D0'),
(5, 'White', 'Nissan', 'M3N4O5'),
(5, 'Brown', 'Kia', 'E1F2G3'),
(6, 'Silver', 'BMW', 'P6Q7R8'),
(6, 'Gray', 'Subaru', 'H4I5J6'),
(7, 'Red', 'Porsche', 'K7L8M9'),
(8, 'Blue', 'Tesla', 'N0O1P2'),
(9, 'Green', 'Mazda', 'Q3R4S5');


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 020b3b7

Please sign in to comment.