From ec5acf7be228c0311f8930ae2756e338652c6622 Mon Sep 17 00:00:00 2001 From: Victor Freire Date: Fri, 12 Aug 2022 14:00:16 -0300 Subject: [PATCH 1/2] Add ability to connect to PostgreSQL via unix socket --- CHANGELOG.md | 1 + app.go | 12 +++++++++--- config-example.yaml | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b63a234..12cbc6dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Updated dependencies (including the library that lacked armhf support) [#722](https://github.com/juanfont/headscale/pull/722) - Fix missing group expansion in function `excludeCorretlyTaggedNodes` [#563](https://github.com/juanfont/headscale/issues/563) - Improve registration protocol implementation and switch to NodeKey as main identifier [#725](https://github.com/juanfont/headscale/pull/725) +- Add ability to connect to PostgreSQL via unix socket [#734](https://github.com/juanfont/headscale/pull/734) ## 0.16.0 (2022-07-25) diff --git a/app.go b/app.go index 3e001203..9f113c2b 100644 --- a/app.go +++ b/app.go @@ -129,13 +129,19 @@ func NewHeadscale(cfg *Config) (*Headscale, error) { switch cfg.DBtype { case Postgres: dbString = fmt.Sprintf( - "host=%s port=%d dbname=%s user=%s password=%s sslmode=disable", + "host=%s dbname=%s user=%s sslmode=disable", cfg.DBhost, - cfg.DBport, cfg.DBname, cfg.DBuser, - cfg.DBpass, ) + + if cfg.DBport != 0 { + dbString += fmt.Sprintf(" port=%d", cfg.DBport) + } + + if cfg.DBpass != "" { + dbString += fmt.Sprintf(" password=%s", cfg.DBpass) + } case Sqlite: dbString = cfg.DBpath default: diff --git a/config-example.yaml b/config-example.yaml index d3d155e2..5360f50f 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -116,6 +116,9 @@ db_path: /var/lib/headscale/db.sqlite # # Postgres config # db_type: postgres # db_host: localhost + +# This should be set when using a TCP connection, set it to '0' or keep it unset if +# connecting through a unix socket. # db_port: 5432 # db_name: headscale # db_user: foo From 0a5db52855373dd6ed950a5f9cd5e6e5595809dd Mon Sep 17 00:00:00 2001 From: Victor Freire Date: Mon, 15 Aug 2022 11:55:38 -0300 Subject: [PATCH 2/2] Add ability to connect to PostgreSQL via unix socket --- config-example.yaml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config-example.yaml b/config-example.yaml index 5360f50f..ed447f9a 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -114,11 +114,9 @@ db_type: sqlite3 db_path: /var/lib/headscale/db.sqlite # # Postgres config +# If using a Unix socket to connect to Postgres, set the socket path in the 'host' field and leave 'port' blank. # db_type: postgres # db_host: localhost - -# This should be set when using a TCP connection, set it to '0' or keep it unset if -# connecting through a unix socket. # db_port: 5432 # db_name: headscale # db_user: foo