@@ -148,6 +148,11 @@ func (n *Node) Init() error {
148148 return fmt .Errorf ("failed to configure postgres %s" , err )
149149 }
150150
151+ fmt .Println ("Configuring pgbouncer auth" )
152+ if err := n .ConfigurePGBouncerAuth (); err != nil {
153+ return fmt .Errorf ("failed to configure pgbouncer auth %s" , err )
154+ }
155+
151156 return nil
152157}
153158
@@ -258,9 +263,24 @@ func (n *Node) PostInit() error {
258263 }
259264 }
260265
266+ primaryIP , err = client .CurrentPrimary ()
267+ if err != nil {
268+ return fmt .Errorf ("failed to query current primary: %s" , err )
269+ }
270+
271+ fmt .Println ("Configuring pgbouncer primary" )
272+ if err := n .ConfigurePGBouncerPrimary (primaryIP , false ); err != nil {
273+ return fmt .Errorf ("failed to configure pgbouncer primary %s" , err )
274+ }
275+
261276 return nil
262277}
263278
279+ func (n * Node ) NewPGBouncerConnection (ctx context.Context ) (* pgx.Conn , error ) {
280+ host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (5432 ))
281+ return openConnection (ctx , host , "pgbouncer" , n .OperatorCredentials )
282+ }
283+
264284func (n * Node ) NewLocalConnection (ctx context.Context ) (* pgx.Conn , error ) {
265285 host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .PGPort ))
266286 return openConnection (ctx , host , "postgres" , n .OperatorCredentials )
@@ -325,6 +345,53 @@ func (n *Node) initializePostgres() error {
325345 return err
326346}
327347
348+ func (n * Node ) ConfigurePGBouncerAuth () error {
349+ path := fmt .Sprintf ("%s/pgbouncer.auth" , "/data" )
350+ file , err := os .OpenFile (path , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0644 )
351+ if err != nil {
352+ return err
353+ }
354+ contents := fmt .Sprintf ("\" %s\" \" %s\" " , n .OperatorCredentials .Username , n .OperatorCredentials .Password )
355+ _ , err = file .Write ([]byte (contents ))
356+ if err != nil {
357+ return err
358+ }
359+ return nil
360+ }
361+
362+ func (n * Node ) ConfigurePGBouncerPrimary (primary string , reload bool ) error {
363+ path := fmt .Sprintf ("%s/pgbouncer.database.ini" , "/data" )
364+ file , err := os .OpenFile (path , os .O_RDWR | os .O_TRUNC | os .O_CREATE , 0644 )
365+ if err != nil {
366+ return err
367+ }
368+ contents := fmt .Sprintf ("[databases]\n * = host=%s port=%d\n " , primary , 5433 )
369+ _ , err = file .Write ([]byte (contents ))
370+ if err != nil {
371+ return err
372+ }
373+
374+ if reload {
375+ err = n .ReloadPGBouncerConfig ()
376+ if err != nil {
377+ fmt .Printf ("failed to reconfigure pgbouncer primary %s\n " , err )
378+ }
379+ }
380+ return nil
381+ }
382+
383+ func (n * Node ) ReloadPGBouncerConfig () error {
384+ conn , err := n .NewPGBouncerConnection (context .TODO ())
385+ if err != nil {
386+ return err
387+ }
388+ _ , err = conn .Exec (context .TODO (), "RELOAD;" )
389+ if err != nil {
390+ return err
391+ }
392+ return nil
393+ }
394+
328395func (n * Node ) configurePostgres () error {
329396 cmdStr := fmt .Sprintf ("sed -i \" s/#shared_preload_libraries.*/shared_preload_libraries = 'repmgr'/\" /data/postgresql/postgresql.conf" )
330397 return runCommand (cmdStr )
0 commit comments