英文:
How to open db connection with user and password
问题
在Golang的sql.Open命令中如何包含PostgreSQL的用户名和密码?下面的代码似乎无法正常工作。谢谢!
func db() (database *sql.DB) {
database, err := sql.Open("postgres", "dbname=chitchat user=tom password=tomtom sslmode=disable")
if err != nil {
log.Fatal(err)
}
return
}
你可以在连接字符串中直接包含用户名和密码。在上面的代码中,用户名是"tom",密码是"tomtom"。你可以根据你的实际情况修改这些值。
英文:
How do I include postgresql username and password in the sql.Open command in golang? The below doesn't seem to work. Thanks!
func db() (database *sql.DB) {
database, err := sql.Open("postgres", "dbname=chitchat user=tom password=tomtom sslmode=disable")
if err != nil {
log.Fatal(err)
}
return
}
答案1
得分: 11
你可以在lib/pq/doc.go或murz/dat(使用lib/pq)中看到示例:
db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
db, err := sql.Open("postgres", "dbname=dat_test user=dat password=!test host=localhost sslmode=disable")
你也可以使用URL连接到数据库。例如:
db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
OP提议:
- 授予对数据库的访问权限
GRANT all privileges on database chitchat to tom;
- 列出
chitchat
数据库中的所有序列:
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
- 授予对每个序列的访问权限
GRANT all privileges on sequence users_id_seq to tom;
GRANT all privileges on sequence threads_id_seq to tom;
GRANT all privileges on sequence posts_id_seq to tom;
GRANT all privileges on sequence sessions_id_seq to tom;
英文:
You can see examples in lib/pq/doc.go or murz/dat (which uses lib/pq):
db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
db, err := sql.Open("postgres", "dbname=dat_test user=dat password=!test host=localhost sslmode=disable")
> You can also connect to a database using a URL. For example:
db, err := sql.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full")
The OP proposes:
> 1. Grant access to database
GRANT all privileges on database chitchat to tom;
2. List all sequences in chitchat
database:
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
3. Grant access to each of them
GRANT all privileges on sequence users_id_seq to tom;
GRANT all privileges on sequence threads_id_seq to tom;
GRANT all privileges on sequence posts_id_seq to tom;
GRANT all privileges on sequence sessions_id_seq to tom;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论