@@ -33,6 +33,7 @@ type Node struct {
33
33
DataDir string
34
34
Region string
35
35
PGPort int
36
+ ProxyPort int
36
37
37
38
SUCredentials Credentials
38
39
OperatorCredentials Credentials
@@ -46,6 +47,7 @@ func NewNode() (*Node, error) {
46
47
node := & Node {
47
48
AppName : "local" ,
48
49
PGPort : 5433 ,
50
+ ProxyPort : 5432 ,
49
51
DataDir : "/data/postgresql" ,
50
52
ManagerDatabaseName : "repmgr" ,
51
53
ManagerConfigPath : "/data/repmgr.conf" ,
@@ -148,6 +150,11 @@ func (n *Node) Init() error {
148
150
return fmt .Errorf ("failed to configure postgres %s" , err )
149
151
}
150
152
153
+ fmt .Println ("Configuring pgbouncer auth" )
154
+ if err := n .ConfigurePGBouncerAuth (); err != nil {
155
+ return fmt .Errorf ("failed to configure pgbouncer auth %s" , err )
156
+ }
157
+
151
158
return nil
152
159
}
153
160
@@ -258,9 +265,24 @@ func (n *Node) PostInit() error {
258
265
}
259
266
}
260
267
268
+ primaryIP , err = client .CurrentPrimary ()
269
+ if err != nil {
270
+ return fmt .Errorf ("failed to query current primary: %s" , err )
271
+ }
272
+
273
+ fmt .Println ("Configuring pgbouncer primary" )
274
+ if err := n .ConfigurePGBouncerPrimary (primaryIP , false ); err != nil {
275
+ return fmt .Errorf ("failed to configure pgbouncer primary %s" , err )
276
+ }
277
+
261
278
return nil
262
279
}
263
280
281
+ func (n * Node ) NewPGBouncerConnection (ctx context.Context ) (* pgx.Conn , error ) {
282
+ host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .ProxyPort ))
283
+ return openConnection (ctx , host , "pgbouncer" , n .OperatorCredentials )
284
+ }
285
+
264
286
func (n * Node ) NewLocalConnection (ctx context.Context ) (* pgx.Conn , error ) {
265
287
host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .PGPort ))
266
288
return openConnection (ctx , host , "postgres" , n .OperatorCredentials )
@@ -325,6 +347,53 @@ func (n *Node) initializePostgres() error {
325
347
return err
326
348
}
327
349
350
+ func (n * Node ) ConfigurePGBouncerAuth () error {
351
+ path := fmt .Sprintf ("%s/pgbouncer.auth" , "/data" )
352
+ file , err := os .OpenFile (path , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0644 )
353
+ if err != nil {
354
+ return err
355
+ }
356
+ contents := fmt .Sprintf ("\" %s\" \" %s\" " , n .OperatorCredentials .Username , n .OperatorCredentials .Password )
357
+ _ , err = file .Write ([]byte (contents ))
358
+ if err != nil {
359
+ return err
360
+ }
361
+ return nil
362
+ }
363
+
364
+ func (n * Node ) ConfigurePGBouncerPrimary (primary string , reload bool ) error {
365
+ path := fmt .Sprintf ("%s/pgbouncer.database.ini" , "/data" )
366
+ file , err := os .OpenFile (path , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0644 )
367
+ if err != nil {
368
+ return err
369
+ }
370
+ contents := fmt .Sprintf ("[databases]\n * = host=%s port=%d\n " , primary , n .PGPort )
371
+ _ , err = file .Write ([]byte (contents ))
372
+ if err != nil {
373
+ return err
374
+ }
375
+
376
+ if reload {
377
+ err = n .ReloadPGBouncerConfig ()
378
+ if err != nil {
379
+ fmt .Printf ("failed to reconfigure pgbouncer primary %s\n " , err )
380
+ }
381
+ }
382
+ return nil
383
+ }
384
+
385
+ func (n * Node ) ReloadPGBouncerConfig () error {
386
+ conn , err := n .NewPGBouncerConnection (context .TODO ())
387
+ if err != nil {
388
+ return err
389
+ }
390
+ _ , err = conn .Exec (context .TODO (), "RELOAD;" )
391
+ if err != nil {
392
+ return err
393
+ }
394
+ return nil
395
+ }
396
+
328
397
func (n * Node ) configurePostgres () error {
329
398
cmdStr := fmt .Sprintf ("sed -i \" s/#shared_preload_libraries.*/shared_preload_libraries = 'repmgr'/\" /data/postgresql/postgresql.conf" )
330
399
return runCommand (cmdStr )
0 commit comments