├── README.md ├── go.mod ├── go.sum └── main.go /README.md: -------------------------------------------------------------------------------- 1 | ## Go With PostgreSql 2 | This is a simple project to test connection between Golang and PostgreSql database nothing else. 3 | 4 | ## Usages 5 | If you want to use this repo in your local machine floww the below instruction. 6 | - I am using [Go](https://go.dev/) version 1.19.4 for this repo make sure you install same version or latest version. 7 | - I am using [pgx](https://github.com/jackc/pgx) v5 third party package to connect with local postgresql. So if you want to download the package in your local machine you have to type the below command: 8 | ```bash 9 | go get github.com/jackc/pgx/v5 10 | ``` 11 | or you can download the package by entering the below command 12 | ``` bash 13 | go mod tidy 14 | ``` 15 | 16 | - Before compile the repo please make sure you have install postgresql on your local machine. 17 | 18 | - To compile the project or see the result in your terminal enter the below command: 19 | ```bash 20 | go run main.go 21 | ``` 22 | 23 | ## Note: 24 | ***Before run the main file please change the database connection uri to your own values. The username, password, host, port, and mydatabase parameters should be set to the correct values for your database.*** 25 | 26 | ## support 27 | if you like my repo and you want more project like this don't forget to give me star. 28 | 29 | *If you have any suggestion for this repository feel free to contribute.* 30 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/go-with-postgresql 2 | 3 | go 1.19 4 | 5 | require ( 6 | github.com/jackc/pgpassfile v1.0.0 // indirect 7 | github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect 8 | github.com/jackc/pgx/v5 v5.2.0 // indirect 9 | golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect 10 | golang.org/x/text v0.3.8 // indirect 11 | ) 12 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 2 | github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= 3 | github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= 4 | github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= 5 | github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= 6 | github.com/jackc/pgx/v5 v5.2.0 h1:NdPpngX0Y6z6XDFKqmFQaE+bCtkqzvQIOt1wvBlAqs8= 7 | github.com/jackc/pgx/v5 v5.2.0/go.mod h1:Ptn7zmohNsWEsdxRawMzk3gaKma2obW+NWTnKa0S4nk= 8 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 9 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 10 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 11 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 12 | golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 h1:Y/gsMcFOcR+6S6f3YeMKl5g+dZMEWqcz5Czj/GWYbkM= 13 | golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= 14 | golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= 15 | golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= 16 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 17 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 18 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "database/sql" 5 | "fmt" 6 | "log" 7 | 8 | _ "github.com/jackc/pgx/v5/stdlib" 9 | ) 10 | 11 | type User struct { 12 | ID int 13 | Name string 14 | Email string 15 | } 16 | 17 | func main() { 18 | // connect to a database 19 | conn, err := sql.Open("pgx", "host=localhost port=5432 dbname=test_connect user=postgres password=password") 20 | 21 | if err != nil { 22 | log.Fatal("Unable to connect", err) 23 | } 24 | 25 | defer conn.Close() 26 | 27 | log.Println("Connected to database") 28 | 29 | // test my connection 30 | 31 | err = conn.Ping() 32 | if err != nil { 33 | log.Fatal("Cannot ping database") 34 | } 35 | 36 | log.Println("Pinged the database") 37 | 38 | //get rows from table 39 | if err = getAllrows(conn); err != nil { 40 | log.Fatal(err) 41 | } 42 | 43 | // insert a row 44 | query := `insert into user_table (name, email) values($1, $2)` 45 | _, err = conn.Exec(query, "Raihan", "raihan@example.com") 46 | 47 | if err != nil { 48 | log.Fatal(err) 49 | } 50 | 51 | log.Println("Inserted a row") 52 | 53 | // get rows from table again 54 | if err = getAllrows(conn); err != nil { 55 | log.Fatal(err) 56 | } 57 | 58 | // update row from table 59 | stmt := `update user_table set name = $1 where name = $2` 60 | 61 | _, err = conn.Exec(stmt, "Abu Raihan", "Raihan") 62 | 63 | if err != nil { 64 | log.Fatal(err) 65 | } 66 | 67 | log.Println("Updated the rows") 68 | 69 | // one row by id 70 | query = `select id, name, email from user_table where name=$1` 71 | 72 | row := conn.QueryRow(query, "Abu Raihan") 73 | var user User 74 | err = row.Scan(&user.ID, &user.Name, &user.Email) 75 | if err != nil { 76 | log.Fatal(err) 77 | } 78 | 79 | log.Println(user) 80 | log.Println("=============") 81 | 82 | // delete one or many row 83 | query = `delete from user_table where name=$1` 84 | _, err = conn.Exec(query, "Abu Raihan") 85 | if err != nil { 86 | log.Println(err) 87 | } 88 | 89 | log.Println("Deleted the user") 90 | 91 | // get rows again 92 | if err = getAllrows(conn); err != nil { 93 | log.Fatal(err) 94 | } 95 | 96 | } 97 | 98 | func getAllrows(conn *sql.DB) error { 99 | rows, err := conn.Query("select id, name, email from user_table") 100 | if err != nil { 101 | 102 | log.Fatal("error no", err) 103 | return err 104 | } 105 | 106 | defer rows.Close() 107 | for rows.Next() { 108 | var user User 109 | err := rows.Scan(&user.ID, &user.Name, &user.Email) 110 | if err != nil { 111 | log.Fatal("error no", err) 112 | return err 113 | } 114 | 115 | fmt.Println("Record is ", user) 116 | } 117 | 118 | if err = rows.Err(); err != nil { 119 | 120 | return err 121 | } 122 | 123 | fmt.Println("========================") 124 | 125 | return nil 126 | } 127 | --------------------------------------------------------------------------------