Skip to content

Commit a0fa95e

Browse files
committed
take better care of pg_hba.conf
1 parent a4fcaa2 commit a0fa95e

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

Diff for: testgres/testgres.py

+41-18
Original file line numberDiff line numberDiff line change
@@ -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\tDATABASE\tUSER\tADDRESS\t\tMETHOD\n"
540-
"local\tall\t\tall\t\t\ttrust\n"
541-
"host\tall\t\tall\t127.0.0.1/32\ttrust\n"
542-
"host\tall\t\tall\t::1/128\t\ttrust\n"
543-
# replication
544-
"local\treplication\tall\t\t\ttrust\n"
545-
"host\treplication\tall\t127.0.0.1/32\ttrust\n"
546-
"host\treplication\tall\t::1/128\t\ttrust\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\treplication\tall\t\t\ttrust\n",
557+
"host\treplication\tall\t127.0.0.1/32\ttrust\n",
558+
"host\treplication\tall\t::1/128\t\ttrust\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

Diff for: tests/test_simple.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_backup_exhaust(self):
248248
pass
249249

250250
def test_backup_and_replication(self):
251-
with get_new_node('node') as node, get_new_node('repl') as replica:
251+
with get_new_node('node') as node:
252252
node.init(allow_streaming=True)
253253
node.start()
254254
node.psql('postgres', 'create table abc(a int, b int)')

0 commit comments

Comments
 (0)