@@ -517,15 +517,18 @@ def init(self, allow_streaming=False, fsync=False, initdb_params=[]):
517
517
518
518
return self
519
519
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' ):
521
524
"""
522
525
Apply default settings to this node.
523
526
524
527
Args:
525
528
allow_streaming: should this node add a hba entry for replication?
526
529
fsync: should this node use fsync to keep data safe?
527
530
log_statement: one of ('all', 'off', 'mod', 'ddl'), look at
528
- postgresql docs for more information
531
+ PostgreSQL docs for more information
529
532
530
533
Returns:
531
534
This instance of PostgresNode.
@@ -534,18 +537,33 @@ def default_conf(self, allow_streaming=False, fsync=False, log_statement='all'):
534
537
postgres_conf = os .path .join (self .data_dir , "postgresql.conf" )
535
538
hba_conf = os .path .join (self .data_dir , "pg_hba.conf" )
536
539
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
549
567
with open (postgres_conf , "w" ) as conf :
550
568
if not fsync :
551
569
conf .write ("fsync = off\n " )
@@ -556,17 +574,22 @@ def default_conf(self, allow_streaming=False, fsync=False, log_statement='all'):
556
574
self .host ,
557
575
self .port ))
558
576
577
+ # replication-related settings
559
578
if allow_streaming :
560
579
cur_ver = LooseVersion (get_pg_version ())
561
580
min_ver = LooseVersion ('9.6' )
562
581
563
582
# select a proper wal_level for PostgreSQL
564
583
wal_level = "hot_standby" if cur_ver < min_ver else "replica"
565
584
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 ))
570
593
571
594
return self
572
595
0 commit comments