{"id":207290,"date":"2025-07-08T08:17:58","date_gmt":"2025-07-08T00:17:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207290\/"},"modified":"2025-07-08T08:17:58","modified_gmt":"2025-07-08T00:17:58","slug":"golang-%e6%90%9c%e7%b4%a2-postgres-%e6%97%b6%e5%87%ba%e7%8e%b0%e6%81%90%e6%85%8c%e5%b4%a9%e6%ba%83","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207290\/","title":{"rendered":"Golang \u641c\u7d22 Postgres \u65f6\u51fa\u73b0\u6050\u614c\u5d29\u6e83"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Golang \u641c\u7d22 Postgres \u65f6\u51fa\u73b0\u6050\u614c\u5d29\u6e83<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 14:36:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300aGolang \u641c\u7d22 Postgres \u65f6\u51fa\u73b0\u6050\u614c\u5d29\u6e83\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6240\u4ee5\u6211\u60f3\u5728\u6211\u6b63\u5728\u7f16\u5199\u7684\u7a0b\u5e8f\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u529f\u80fd\u3002\u57fa\u672c\u4e0a\uff0c\u5982\u679c\u7528\u6237\u6709 cookie \u8868\u660e\u4ed6\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u662f\u4ec0\u4e48\uff0c\u5b83\u5c06\u5728\u5b58\u50a8\u7684 postgres \u6570\u636e\u5e93\u4e2d\u67e5\u627e\u7528\u6237\u540d\u548c cookie \u7684\u6563\u5217\u503c\uff0c\u5982\u679c\u6211\u5f97\u5230\u5339\u914d\uff0c\u5219\u8fd4\u56de\u80af\u5b9a\u54cd\u5e94\u548c jwt \u4ee4\u724c\uff08\u5b89\u5168\u6027\u4e3a\u6211\u53ef\u4ee5\u4f7f\u7528 bcrypt \u54c8\u5e0c &#8211; \u5b58\u50a8\u54c8\u5e0c\u5bc6\u7801\u662f\u5426\u662f\u4e00\u4e2a\u597d\u4e3b\u610f\u53ef\u80fd\u662f\u53e6\u4e00\u4e2a\u95ee\u9898\uff09\u3002 <\/p>\n<p>\u6bcf\u6b21\u7528\u6237\u8bbf\u95ee\u9700\u8981\u4fdd\u5b58\u5bc6\u7801\u7684\u9875\u9762\u65f6\uff0c\u90fd\u4f1a\u5411\u670d\u52a1\u5668\u53d1\u51fa\u6b64\u8bf7\u6c42\u3002 <\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\u6211\u65f6\u4e0d\u65f6\u4f1a\u6536\u5230\u4ee5\u4e0b\u4fe1\u606f<\/p>\n<pre>inside cookielogin\napp_1  | search_userinfo_table started\ndb_1   | fatal:  sorry, too many clients already\napp_1  | 2018\/08\/26 16:22:25 pq: sorry, too many clients already\napp_1  | 2018\/08\/26 16:22:25 http: panic serving 172.21.0.1:37304: pq: sorry, too many clients already\napp_1  | goroutine 704 [running]:\napp_1  | net\/http.(*conn).serve.func1(0xc420156f00)\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:1721 +0xd0\napp_1  | panic(0x6d63e0, 0xc420102c20)\napp_1  |    \/usr\/local\/go\/src\/runtime\/panic.go:489 +0x2cf\napp_1  | log.panic(0xc4200438c0, 0x1, 0x1)\napp_1  |    \/usr\/local\/go\/src\/log\/log.go:322 +0xc0\napp_1  | github.com\/patientplatypus\/webserver\/database.search_userinfo_table(0xc42017f29d, 0xb, 0xc4201500c0, 0xc4200c22e0, 0xc420043a10)\napp_1  |    \/go\/src\/github.com\/patientplatypus\/webserver\/database\/search.go:18 +0x169\napp_1  | github.com\/patientplatypus\/webserver\/authentication.loginhandler(0xc42017f29d, 0xb, 0xc42017f2ba, 0x3, 0x899a80, 0xc420148460)\napp_1  |    \/go\/src\/github.com\/patientplatypus\/webserver\/authentication\/login.go:53 +0x39\napp_1  | github.com\/patientplatypus\/webserver\/authentication.cookielogin(0x899a80, 0xc420148460, 0xc4203fc800)\napp_1  |    \/go\/src\/github.com\/patientplatypus\/webserver\/authentication\/login.go:35 +0x279\napp_1  | net\/http.handlerfunc.servehttp(0x75c658, 0x899a80, 0xc420148460, 0xc4203fc800)\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:1942 +0x44\napp_1  | github.com\/gorilla\/mux.(*router).servehttp(0xc420052460, 0x899a80, 0xc420148460, 0xc4203fc800)\napp_1  |    \/go\/src\/github.com\/gorilla\/mux\/mux.go:162 +0x101\napp_1  | main.jwthandler.func1(0x899a80, 0xc420148460, 0xc4203fc600)\napp_1  |    \/go\/src\/github.com\/patientplatypus\/webserver\/main.go:34 +0x342\napp_1  | net\/http.handlerfunc.servehttp(0xc4200b12a0, 0x899a80, 0xc420148460, 0xc4203fc600)\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:1942 +0x44\napp_1  | github.com\/rs\/cors.(*cors).handler.func1(0x899a80, 0xc420148460, 0xc4203fc600)\napp_1  |    \/go\/src\/github.com\/rs\/cors\/cors.go:200 +0xe9\napp_1  | net\/http.handlerfunc.servehttp(0xc4200b12c0, 0x899a80, 0xc420148460, 0xc4203fc600)\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:1942 +0x44\napp_1  | net\/http.serverhandler.servehttp(0xc42008a630, 0x899a80, 0xc420148460, 0xc4203fc600)\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:2568 +0x92\napp_1  | net\/http.(*conn).serve(0xc420156f00, 0x899fc0, 0xc4200506c0)\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:1825 +0x612\napp_1  | created by net\/http.(*server).serve\napp_1  |    \/usr\/local\/go\/src\/net\/http\/server.go:2668 +0x2ce<\/pre>\n<p>&#8230;\u7136\u540e golang \u5ba2\u6237\u7aef\u5c31\u4f1a\u5d29\u6e83\uff0c\u6211\u5c06\u4e0d\u5f97\u4e0d\u624b\u52a8\u91cd\u65b0\u542f\u52a8\u3002\u8fd9\u662f\u4e00\u4e2a\u4ec5<em>\u6709\u65f6<\/em>\u53d1\u751f\u7684\u9519\u8bef\uff0c\u8fd9\u975e\u5e38\u4ee4\u4eba\u6cae\u4e27\uff0c\u56e0\u4e3a\u5b83\u4f7f\u8c03\u8bd5\u53d8\u5f97\u56f0\u96be\uff08\u62b1\u6b49\uff0c\u7ec8\u7aef\u65e5\u5fd7\u5f88\u8be6\u7ec6 &#8211; \u6211\u4e0d\u786e\u5b9a\u9519\u8bef\u4f55\u65f6\u4f1a\u518d\u6b21\u89e6\u53d1\uff09\u3002<\/p>\n<p>\u5927\u5bb6\u6709\u4ec0\u4e48\u610f\u89c1\u6216\u5efa\u8bae\u5417\uff1f\u8fd9\u662f no-duh \u89e3\u51b3\u65b9\u6848\u7684\u5e38\u89c1\u95ee\u9898\u5417\uff1f<\/p>\n<p>\u8c22\u8c22\uff01<\/p>\n<p>\u7f16\u8f91\uff1a<\/p>\n<p>\u636e\u6211\u6240\u77e5\uff0c\u8fd9\u662f\u6709\u95ee\u9898\u7684 postgres \u641c\u7d22\u67e5\u8be2\uff1a<\/p>\n<pre>package data\n\nimport(\n    \"fmt\"\n    \"log\"\n    _ \"github.com\/lib\/pq\"\n)\n\n\nfunc Search_userinfo_table(searchEmail string)(bool, string) {\n    fmt.Println(\"search_userinfo_table started\")\n    rows, err1 := db.Query(\"SELECT * FROM userinfo\")\n    if err1 != nil {\n        log.Panic(err1)\n    }\n    for rows.Next() {\n        var email string\n        var password string\n        var regString string\n        var regBool bool\n        var uid int\n        err2 := rows.Scan(&amp;email, &amp;password, &amp;regString, &amp;regBool, &amp;uid)\n        if err2 != nil {\n            log.Panic(err2)\n        }\n        fmt.Println(\"email | password\")\n        fmt.Printf(\"%s | %s\", email, password)\n        if email == searchEmail{\n            return false, password\n        }\n    }\n    return true, \"\"\n}<\/pre>\n<p>\u5b83\u8fd4\u56de\u7528\u6237\u662f\u5426\u5728\u6570\u636e\u5e93\u4e2d\u4ee5\u53ca\u54c8\u5e0c\u5bc6\u7801\u3002\u4e0e\u4e00\u4e9b\u4eba\u4ea4\u8c08\uff0c\u7b54\u6848\u4f3c\u4e4e\u662f postgres \u6bcf\u6b21\u8fdb\u884c\u6b64\u8bfb\u53d6\u65f6\u90fd\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u65b0\u8fde\u63a5\uff0c\u7136\u540e\u6700\u7ec8\u5d29\u6e83\uff0c\u4f46\u6211\u5e0c\u671b\u5b83\u91cd\u7528\u8fde\u63a5\uff08\u5c3d\u7ba1\u6211\u8ba4\u4e3a\u8fd9\u662f\u81ea\u52a8\u53d1\u751f\u7684\uff1f\uff09\u3002\u65e0\u8bba\u5982\u4f55\uff0c\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7f3a\u5c11\u6240\u9700\u7684 <code>defer \u884c\u3002Close()<\/code>\u3002<\/p>\n<p><code>*sql.Rows<\/code> \u7684\u6bcf\u4e2a\u5b9e\u4f8b\u90fd\u6709\u4e00\u4e2a\u4e0e\u5176\u5173\u8054\u7684\u8fde\u63a5\u3002\u5f53\u60a8\u5173\u95ed\u884c\u5b9e\u4f8b\u65f6\uff0c\u5173\u8054\u7684\u8fde\u63a5\u5c06\u88ab\u91ca\u653e\u5e76\u653e\u56de\u6c60\u4e2d (<code>*DB<\/code>)\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u4e0d\u5173\u95ed Rows \u5b9e\u4f8b\u5c06\u5bfc\u81f4\u5176\u8fde\u63a5\u4e0d\u662f \u3002\u5e76\u4e14\u4e0d\u91ca\u653e\u8fde\u63a5\u5c06\u5bfc\u81f4\u8fde\u63a5\u6c60\u5c1d\u8bd5\u6253\u5f00\u8fc7\u591a\u7684\u65b0\u8fde\u63a5\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aGolang \u641c\u7d22 Postgres \u65f6\u51fa\u73b0\u6050\u614c\u5d29\u6e83\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207290","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207290"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207290\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}