@@ -33,6 +33,7 @@ type Node struct {
3333 DataDir string
3434 Region string
3535 PGPort int
36+ ProxyPort int
3637
3738 SUCredentials Credentials
3839 OperatorCredentials Credentials
@@ -46,6 +47,7 @@ func NewNode() (*Node, error) {
4647 node := & Node {
4748 AppName : "local" ,
4849 PGPort : 5433 ,
50+ ProxyPort : 5432 ,
4951 DataDir : "/data/postgresql" ,
5052 ManagerDatabaseName : "repmgr" ,
5153 ManagerConfigPath : "/data/repmgr.conf" ,
@@ -148,6 +150,11 @@ func (n *Node) Init() error {
148150 return fmt .Errorf ("failed to configure postgres %s" , err )
149151 }
150152
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+
151158 return nil
152159}
153160
@@ -258,9 +265,24 @@ func (n *Node) PostInit() error {
258265 }
259266 }
260267
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+
261278 return nil
262279}
263280
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+
264286func (n * Node ) NewLocalConnection (ctx context.Context ) (* pgx.Conn , error ) {
265287 host := net .JoinHostPort (n .PrivateIP , strconv .Itoa (n .PGPort ))
266288 return openConnection (ctx , host , "postgres" , n .OperatorCredentials )
@@ -325,6 +347,53 @@ func (n *Node) initializePostgres() error {
325347 return err
326348}
327349
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+
328397func (n * Node ) configurePostgres () error {
329398 cmdStr := fmt .Sprintf ("sed -i \" s/#shared_preload_libraries.*/shared_preload_libraries = 'repmgr'/\" /data/postgresql/postgresql.conf" )
330399 return runCommand (cmdStr )
0 commit comments