{"id":206955,"date":"2025-07-08T08:18:44","date_gmt":"2025-07-08T00:18:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206955\/"},"modified":"2025-07-08T08:18:44","modified_gmt":"2025-07-08T00:18:44","slug":"pgxpool-connect-%e8%bf%94%e5%9b%9e%e7%9a%84%e6%b1%a0%e4%b8%ba-nil-%e6%88%96%e5%bf%ab%e9%80%9f%e5%8f%98%e4%b8%ba-nil-%e8%80%8c%e4%b8%8d%e4%bc%9a%e5%87%ba%e7%8e%b0%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206955\/","title":{"rendered":"pgxpool.Connect \u8fd4\u56de\u7684\u6c60\u4e3a nil \u6216\u5feb\u901f\u53d8\u4e3a nil \u800c\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef"},"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>pgxpool.Connect \u8fd4\u56de\u7684\u6c60\u4e3a nil \u6216\u5feb\u901f\u53d8\u4e3a nil \u800c\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-18 23:51:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300apgxpool.Connect \u8fd4\u56de\u7684\u6c60\u4e3a nil \u6216\u5feb\u901f\u53d8\u4e3a nil \u800c\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u4ee5\u4e0b\u7528\u4e8e\u8fde\u63a5\u5230 postgres \u6570\u636e\u5e93\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>func connecttopostgres(ctx context.context, url string) (*pgxpool.pool, error) {\n    var err error\n    for i := 0; i &lt; 5; i++ {\n        p, err := pgxpool.connect(ctx, url)\n        if err != nil || p == nil {\n            time.sleep(3 * time.second)\n            continue\n        }\n        log.printf(\"pool returned from connect: %s\", p)\n        return p, nil\n    }\n    return nil, errors.wrap(err, \"timed out waiting to connect postgres\")\n}<\/pre>\n<p>\u7528\u4f8b\u662f\u5728\u4f7f\u7528 docker-compose \u542f\u52a8\u6211\u7684\u670d\u52a1\u5668\u65f6\u7b49\u5f85 postgres \u53d8\u5f97\u53ef\u7528\u3002\u5373\u4f7f\u5982\u679c <code>p == nil<\/code> \u4ee3\u7801\u4f1a\u4f11\u7720\uff0c\u7b2c\u4e00\u6b21\u8fd4\u56de\u4e4b\u524d\u7684\u65e5\u5fd7\u4e5f\u4f1a\u6253\u5370\u51fa\u6765\uff1a <code>pool returned from connect: %!s(*pgxpool.pool=&lt;nil&gt;)<\/code><\/p>\n<p><code>pgxpool<\/code> \u4e2d\u7684\u540e\u53f0\u8fdb\u7a0b\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u4f7f <code>p == nil<\/code> \uff1f<\/p>\n<p>\u5bf9\u4e8e\u4e3a\u4ec0\u4e48\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u6709\u4ec0\u4e48\u60f3\u6cd5\u5417\uff1f<\/p>\n<p>\u7f16\u8f91\uff1a\u8fd9\u4f3c\u4e4e\u53ea\u5728\u901a\u8fc7 docker-compose \u8fd0\u884c\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u548c postgres \u65f6\u53d1\u751f\u3002\u6211\u6b63\u5728\u4f7f\u7528\u4ee5\u4e0b\u64b0\u5199\u6587\u4ef6\uff1a<\/p>\n<pre>services:\n    app:\n        build: .\n        ports:\n            - \"8080:8080\"\n        depends_on:\n            - \"db\"\n\n    db:\n        image: postgres\n        restart: always\n        environment:\n            - postgres_db=demo_db\n            - postgres_password=${postgres_password}\n        ports:\n            - \"8081:5432\"<\/pre>\n<p>\u4ee5\u53ca\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u7684 dockerfile\uff1a<\/p>\n<pre>from golang:1.17\n\nworkdir \/\n\ncopy go.mod .\ncopy go.sum .\ncopy *.go .\n\nrun go mod download\nrun go build\n\nexpose 8080\n\ncmd [ \".\/app\" ]<\/pre>\n<p>\u4ee5\u53ca\u4e00\u4e2a\u6700\u4f4e\u9650\u5ea6\u53ef\u91cd\u590d\u7684\u793a\u4f8b go \u6587\u4ef6\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"log\"\n    \"net\/http\"\n    \"time\"\n\n    \"github.com\/jackc\/pgx\/v4\/pgxpool\"\n    \"github.com\/pkg\/errors\"\n)\n\nfunc main() {\n    log.Printf(\"connecting to postgres...\")\n    pgpool, err := connectToPostgres(context.Background(), \"postgresql:\/\/localhost:5432\/demo_db\")\n    log.Printf(\"pool: %s\", pgpool)\n    if err != nil {\n        log.Fatalln(err)\n    }\n    log.Printf(\"successfully connected to postgres\")\n\n    if err := http.ListenAndServe(\":8080\", nil); err != nil {\n        log.Fatal(err)\n    }\n    log.Println(\"stopped\")\n}\n\nfunc connectToPostgres(ctx context.Context, url string) (*pgxpool.Pool, error) {\n    var err error\n    for i := 0; i &lt; 5; i++ {\n        p, err := pgxpool.Connect(ctx, url)\n        if err != nil || p == nil {\n            time.Sleep(3 * time.Second)\n            continue\n        }\n        log.Printf(\"pool returned from connect: %s\", p)\n        return p, nil\n    }\n    return nil, errors.Wrap(err, \"timed out waiting to connect postgres\")\n}<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u662f\uff0c\u5728 <code>docker-compose<\/code> \u7f51\u7edc\u4e2d\u8fde\u63a5\u65f6\uff0c\u60a8\u5fc5\u987b\u8fde\u63a5\u5230\u5bb9\u5668\u7684\u4e3b\u673a\u540d\uff0c\u5728\u672c\u4f8b\u4e2d\u4e3a <code>db<\/code>\u3002<\/p>\n<p>\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5bb9\u5668\u7684 <code>ip<\/code> \u4f46\u9700\u8981\u989d\u5916\u7684\u5de5\u4f5c\u91cf\uff0c\u4ec5\u4f7f\u7528\u4e3b\u673a\u540d\u4f1a\u66f4\u7b80\u5355\u3002<\/p>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u4f60\u7684\u8fde\u63a5\u5b57\u7b26\u4e32\u9519\u8bef\uff0c\u6211\u5728\u8fde\u63a5\u5230 <code>localhost<\/code> \u65f6\u4e5f\u5f97\u5230\u4e86\u8fd9\u4e2a<\/p>\n<pre>app_1  | 2021\/12\/21 18:53:28 pool: %!s(*pgxpool.pool=&lt;nil&gt;)\napp_1  | 2021\/12\/21 18:53:28 successfully connected to postgres<\/pre>\n<p>\u4f7f\u7528\u6b63\u786e\u7684\u8fde\u63a5\u5b57\u7b26\u4e32\u8fde\u63a5\u65f6\uff1a<\/p>\n<pre>\"postgres:\/\/postgres:mysecretpassword@db:5432\/postgres\"<\/pre>\n<p>\u5b83\u5de5\u4f5c\u5f97\u5f88\u597d\u3002<\/p>\n<p>\u5176\u4f59\u65e5\u5fd7<\/p>\n<pre>db_1   | 2021-12-21 18:56:04.122 UTC [1] LOG:  database system is ready to accept connections\napp_1  | 2021\/12\/21 18:56:06 pool returned from connect: &amp;{%!s(*puddle.Pool=&amp;{0xc00007c040 0xc0000280b0 [0xc00007c0c0] [0xc00007c0c0] 0x65cb60 0x65dc80 16 1 9872796 1 0 false}) %!s(*pgxpool.Config=&amp;{0xc0000a2000 &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; 3600000000000 1800000000000 16 0 60000000000 false true}) %!s(func(context.Context, *pgx.ConnConfig) error=&lt;nil&gt;) %!s(func(context.Context, *pgx.Conn) error=&lt;nil&gt;) %!s(func(context.Context, *pgx.Conn) bool=&lt;nil&gt;) %!s(func(*pgx.Conn) bool=&lt;nil&gt;) %!s(int32=0) %!s(time.Duration=3600000000000) %!s(time.Duration=1800000000000) %!s(time.Duration=60000000000) {%!s(uint32=0) {%!s(int32=0) %!s(uint32=0)}} %!s(chan struct {}=0xc000024060)}\napp_1  | 2021\/12\/21 18:56:06 pool: &amp;{%!s(*puddle.Pool=&amp;{0xc00007c040 0xc0000280b0 [0xc00007c0c0] [0xc00007c0c0] 0x65cb60 0x65dc80 16 1 9872796 1 0 false}) %!s(*pgxpool.Config=&amp;{0xc0000a2000 &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; 3600000000000 1800000000000 16 0 60000000000 false true}) %!s(func(context.Context, *pgx.ConnConfig) error=&lt;nil&gt;) %!s(func(context.Context, *pgx.Conn) error=&lt;nil&gt;) %!s(func(context.Context, *pgx.Conn) bool=&lt;nil&gt;) %!s(func(*pgx.Conn) bool=&lt;nil&gt;) %!s(int32=0) %!s(time.Duration=3600000000000) %!s(time.Duration=1800000000000) %!s(time.Duration=60000000000) {%!s(uint32=0) {%!s(int32=0) %!s(uint32=0)}} %!s(chan struct {}=0xc000024060)}\napp_1  | 2021\/12\/21 18:56:06 successfully connected to postgres<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300apgxpool.Connect \u8fd4\u56de\u7684\u6c60\u4e3a nil \u6216\u5feb\u901f\u53d8\u4e3a nil \u800c\u4e0d\u4f1a\u51fa\u73b0\u9519\u8bef\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\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-206955","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206955","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=206955"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206955\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}