生命不止,继续 Go go go !!!
Go实战–go语言操作MySQL数据库(go-sql-driver/mysql)
Go实战–go语言操作sqlite数据库(The way to go)
Go实战–golang中使用redis(redigo和go-redis/redis)
今天跟大家分享golang中使用Postgresql数据库。
何为Postgresql
官网
https://www.postgresql.org/
Postgresql is a powerful,open source object-relational database system.
Postgresql是一个功能强大的开源对象关系数据库管理系统(ORDBMS)。 用于安全地存储数据; 支持最佳做法,并允许在处理请求时检索它们。
特点
Postgresql可在所有主要操作系统(即Linux,UNIX(AIX,BSD,HP-UX,SGI IRIX,Mac OS X,Solaris,Tru64)和Windows等)上运行
Postgresql支持文本,图像,声音和视频,并包括用于C/C++,Java,Perl,Python,Ruby,Tcl和开放数据库连接(ODBC)的编程接口
Postgresql支持sql的许多功能,例如复杂SQL查询,sql子选择,外键,触发器,视图,事务,多进程并发控制(MVCC),流式复制(9.0),热备(9.0))
在Postgresql中,表可以设置为从“父”表继承其特征
Postgresql 与 MysqL 相比,优势何在?
知乎上有大神们的讨论,可以看一看:https://www.zhihu.com/question/20010554
Windows下安装Postgresql
下载
https://www.postgresql.org/download/windows/
跳转到https://www.enterprisedb.com/downloads/postgres-postgresql-downloads#windows
根据自己的操作系统下载,例如我选择:
Postgresql 9.6.4
Windows x86-64
下载速度不是很快,稍安勿躁。
安装
下一步,选择安装文件夹,选择数据所在位置,填写密码,端口号,选择运行时语言环境等。
默默等待安装。
使用pgAdmin
打开pgAdmin,要输入之前设置的密码
创建数据库
在Databases(1)上右键,Create,Database
创建表
插入数据
使用sql Shell(psql)
创建数据库
create database name;
drop database name;
创建表
CREATE TABLE public.student ( id integer,name character(1)[] COLLATE pg_catalog."default" ) WITH ( OIDS = FALSE ) TABLESPACE pg_default;
ALTER TABLE public.student OWNER to postgres;
插入数据
INSERT INTO public.teacher(
id,age)
VALUES (2, 34);
SELECT * FROM public.teacher;
Go中使用Postgresql
github.com/lib/pq
Pure Go Postgres driver for database/sql
连接
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "your_password"
dbname = "test"
)
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",host,port,user,password,dbname)
db,err := sql.Open("postgres",psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
插入
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "wangshubo123"
dbname = "test"
)
type Teacher struct {
ID int
Age int
}
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
sqlStatement := ` INSERT INTO teacher (id,age) VALUES ($1,$2) RETURNING id`
id := 3
err = db.QueryRow(sqlStatement, 3, 19).Scan(&id)
if err != nil {
panic(err)
}
fmt.Println("New record ID is:",id)
}
再运行一次,错误:panic: pq: 重复键违反唯一约束”teacher_pkey”
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "wangshubo123"
dbname = "test"
)
type Teacher struct {
ID int
Age int
}
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
sqlStatement := `SELECT * FROM teacher WHERE id=$1;`
var teacher Teacher
row := db.QueryRow(sqlStatement, 1)
err = row.Scan(&teacher.ID,&teacher.Age)
switch err {
case sql.ErrNoRows:
fmt.Println("No rows were returned!")
return
case nil:
fmt.Println(teacher)
default:
panic(err)
}
}