与 postgreSQL 模式连接
Connect with postgreSQL schema
我希望连接并查询 PostgreSQL。但我只想连接到特定的 Schema.
根据文档 (JDBC) 我们可以使用
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
或更新 从 9.4 开始,您可以使用新的 currentSchema 参数指定 url,如下所示:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
但是我无法使用 golang SQL 驱动程序;
根据文档,我们也可以使用 SET search_path TO myschema,public;
但我只想在初始化期间声明一次,但我认为每次新连接都需要执行一次。
此外,我正在使用以下代码,请帮助我确定要传递给它的正确参数,以便仅与模式连接
db, err := sql.Open("postgres", `dbname=`+s.settings.Database+
` user=`+s.settings.Username+` password=`+s.settings.Password+
` host=`+s.settings.Url+` sslmode=disable`)
添加 currentSchema=myschema
或 searchpath=myschema
无效!
有没有一种方法我只能连接到 GO 中的特定数据库模式
集Search_path是对的,你做一次。即:
db, err := sql.Open("postgres",
"host=localhost dbname=Test sslmode=disable user=postgres password=secret")
if err != nil {
log.Fatal("cannot connect ...")
}
defer db.Close()
db.Exec(`set search_path='mySchema'`)
rows, err := db.Query(`select blah,blah2 from myTable`)
...
您应该将 search_path=myschema
添加到 dataSourceName
P.S。最好使用 fmt.Sprintf("host=%s port=%d dbname=%s user=%s password='%s' sslmode=disable search_path=%s", ...)
而不是 ``+``
我希望连接并查询 PostgreSQL。但我只想连接到特定的 Schema.
根据文档 (JDBC) 我们可以使用
jdbc:postgresql://localhost:5432/mydatabase?searchpath=myschema
或更新 从 9.4 开始,您可以使用新的 currentSchema 参数指定 url,如下所示:
jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
但是我无法使用 golang SQL 驱动程序;
根据文档,我们也可以使用 SET search_path TO myschema,public;
但我只想在初始化期间声明一次,但我认为每次新连接都需要执行一次。
此外,我正在使用以下代码,请帮助我确定要传递给它的正确参数,以便仅与模式连接
db, err := sql.Open("postgres", `dbname=`+s.settings.Database+
` user=`+s.settings.Username+` password=`+s.settings.Password+
` host=`+s.settings.Url+` sslmode=disable`)
添加 currentSchema=myschema
或 searchpath=myschema
无效!
有没有一种方法我只能连接到 GO 中的特定数据库模式
集Search_path是对的,你做一次。即:
db, err := sql.Open("postgres",
"host=localhost dbname=Test sslmode=disable user=postgres password=secret")
if err != nil {
log.Fatal("cannot connect ...")
}
defer db.Close()
db.Exec(`set search_path='mySchema'`)
rows, err := db.Query(`select blah,blah2 from myTable`)
...
您应该将 search_path=myschema
添加到 dataSourceName
P.S。最好使用 fmt.Sprintf("host=%s port=%d dbname=%s user=%s password='%s' sslmode=disable search_path=%s", ...)
而不是 ``+``