database - 在 go postgres 中连接到多个数据库的最佳方法

标签 database postgresql go

我正在开发一个网站构建器,并将每个网站数据存储在单独的数据库中。
我的问题是如何正确有效地处理多个数据库连接。 所有数据库和代码都在同一服务器中

最佳答案

我创建了自己的方式来连接到多个连接数据库。

首先,我为 postgre 创建基本文件:

type PostgreHost struct {
    Driver   string
    Database string
    Username string
    Ssl      string
    Password string
}

type PostgreSystem interface {
    Init()
    Connect() (*sqlx.DB, error)
}

var logger *log.Logger

func (p PostgreHost) Init() {
    logger = log.New(os.Stderr,
        "Postgre",
        log.Ldate|log.Ltime|log.Lshortfile)
}

func (p *PostgreHost) Connect() (*sqlx.DB, error) {
    connection := fmt.Sprintf("user=%v password= %v dbname=%v sslmode=%v", p.Username, p.Password, p.Database, p.Ssl)
    db, err := sqlx.Connect(
        p.Driver,
        connection)
    if err != nil {
        logger.Fatal(err)
        return nil, err
    }

    return db, nil
}

func GetPostgreDb(postgre PostgreSystem) (*sqlx.DB, error) {
    return postgre.Connect()
}

然后调用它来创建连接列表,例如 this :

//we create different types of databse connection here
func SystemConnection() map[string]interface{} {
    listConnection := make(map[string]interface{})
    var err error
    // create redis connection
    redisConn := RedisHost{
        Address:  "localhost:6379",
        Password: "",
        DB:       0,
    }

    redisConnection, err := redisConn.Connect()
    if err != nil {
        panic(err)
    }

    // create postgre connection
    postgreConn := PostgreHost{
        Driver:   "postgres",
        Database: "postgres",
        Username: "postgres",
        Ssl:      "disable",
        Password: "root",
    }
   // you can create your another connection here : 
   postgreConn2 := PostgreHost{
        Driver:   "postgres",
        Database: "postgres",
        Username: "postgres",
        Ssl:      "disable",
        Password: "root",
    }

    postgreConnection, err := GetPostgreDb(&postgreConn)

    if err != nil {
        panic(err)
    }

    postgreConnection2, err := GetPostgreDb(&postgreConn2)

    if err != nil {
        panic(err)
    }

    listConnection["redis"] = redisConnection
    listConnection["postgre"] = postgreConnection
    listConnection["postgre2"] = postgreConnection2
    return listConnection
}

最后从 map 上调用所有连接:

    //getting list of all the connection.
    listConnection := database.SystemConnection()

    //getting redis connection convert it from interface to *redisClient.
    redisConn := listConnection["redis"].(*redis.Client)

    // get postgre connection.
    postgreConn := listConnection["postgre"].(*sqlx.DB)
    postgreConn2 := listConnection["postgre2"].(*sqlx.DB)

您可以从 here 获取所有源代码。它仍然在进步,但希望你能明白这个想法。希望对您有所帮助。

关于database - 在 go postgres 中连接到多个数据库的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41699630/

相关文章:

sql - 如何在 PostgreSQL 中通过数组正确创建多个条目?

json - Django 自动将文本字段中的双引号转换为单引号

mongodb - Go:如何对与Mongo集合兼容的代码进行单元测试?

json - Golang net/http 请求 Body 总是空的

database - 如何在flutter中从SQLITE表中获取不同记录?

sql-server - SQL Server - 数据库 'Database' 不存在。确保名称输入正确

mysql - 如何汇总 MySQL 表中的连续行

sql - 将架构名称添加到动态 Postgres 查询

postgresql - PostgreSQL 中 && 运算符的时间复杂度

go - 独立安装文件,例如 go run myfile.go