@@ -517,15 +517,18 @@ def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
517517
518518 return self
519519
520- def default_conf (self , allow_streaming = False , fsync = False , log_statement = 'all' ):
520+ def default_conf (self ,
521+ allow_streaming = False ,
522+ fsync = False ,
523+ log_statement = 'all' ):
521524 """
522525 Apply default settings to this node.
523526
524527 Args:
525528 allow_streaming: should this node add a hba entry for replication?
526529 fsync: should this node use fsync to keep data safe?
527530 log_statement: one of ('all', 'off', 'mod', 'ddl'), look at
528- postgresql docs for more information
531+ PostgreSQL docs for more information
529532
530533 Returns:
531534 This instance of PostgresNode.
@@ -534,18 +537,33 @@ def default_conf(self, allow_streaming=False, fsync=False, log_statement='all'):
534537 postgres_conf = os .path .join (self .data_dir , "postgresql.conf" )
535538 hba_conf = os .path .join (self .data_dir , "pg_hba.conf" )
536539
537- # add parameters to hba file
538- with open (hba_conf , "w" ) as conf :
539- conf .write ("# TYPE\t DATABASE\t USER\t ADDRESS\t \t METHOD\n "
540- "local\t all\t \t all\t \t \t trust\n "
541- "host\t all\t \t all\t 127.0.0.1/32\t trust\n "
542- "host\t all\t \t all\t ::1/128\t \t trust\n "
543- # replication
544- "local\t replication\t all\t \t \t trust\n "
545- "host\t replication\t all\t 127.0.0.1/32\t trust\n "
546- "host\t replication\t all\t ::1/128\t \t trust\n " )
547-
548- # add parameters to config file
540+ # filter lines in hba file
541+ with open (hba_conf , "r+" ) as conf :
542+ # get rid of comments and blank lines
543+ lines = [
544+ s for s in conf .readlines ()
545+ if len (s .strip ()) > 0 and not s .startswith ('#' )
546+ ]
547+
548+ # write filtered lines
549+ conf .seek (0 )
550+ conf .truncate ()
551+ conf .writelines (lines )
552+
553+ # replication-related settings
554+ if allow_streaming :
555+ new_lines = [
556+ "local\t replication\t all\t \t \t trust\n " ,
557+ "host\t replication\t all\t 127.0.0.1/32\t trust\n " ,
558+ "host\t replication\t all\t ::1/128\t \t trust\n "
559+ ]
560+
561+ # write missing lines
562+ for line in new_lines :
563+ if line not in lines :
564+ conf .write (line )
565+
566+ # overwrite postgresql.conf file
549567 with open (postgres_conf , "w" ) as conf :
550568 if not fsync :
551569 conf .write ("fsync = off\n " )
@@ -556,17 +574,22 @@ def default_conf(self, allow_streaming=False, fsync=False, log_statement='all'):
556574 self .host ,
557575 self .port ))
558576
577+ # replication-related settings
559578 if allow_streaming :
560579 cur_ver = LooseVersion (get_pg_version ())
561580 min_ver = LooseVersion ('9.6' )
562581
563582 # select a proper wal_level for PostgreSQL
564583 wal_level = "hot_standby" if cur_ver < min_ver else "replica"
565584
566- conf .write ("max_wal_senders = 5\n "
567- "wal_keep_segments = 20\n "
568- "hot_standby = on\n "
569- "wal_level = {}\n " .format (wal_level ))
585+ max_wal_senders = 5
586+ wal_keep_segments = 20
587+ conf .write ("hot_standby = on\n "
588+ "max_wal_senders = {}\n "
589+ "wal_keep_segments = {}\n "
590+ "wal_level = {}\n " .format (max_wal_senders ,
591+ wal_keep_segments ,
592+ wal_level ))
570593
571594 return self
572595
0 commit comments