@@ -131,6 +131,23 @@ def start(self, wait=60, *, server_settings={}, **opts):
131131
132132 self ._test_connection (timeout = wait )
133133
134+ def reload (self ):
135+ """Reload server configuration."""
136+ status = self .get_status ()
137+ if status != 'running' :
138+ raise ClusterError ('cannot reload: cluster is not running' )
139+
140+ process = subprocess .run (
141+ [self ._pg_ctl , 'reload' , '-D' , self ._data_dir ],
142+ stdout = subprocess .PIPE , stderr = subprocess .PIPE )
143+
144+ stderr = process .stderr
145+
146+ if process .returncode != 0 :
147+ raise ClusterError (
148+ 'pg_ctl stop exited with status {:d}: {}' .format (
149+ process .returncode , stderr .decode ()))
150+
134151 def stop (self , wait = 60 ):
135152 process = subprocess .run (
136153 [self ._pg_ctl , 'stop' , '-D' , self ._data_dir , '-t' , str (wait ),
@@ -165,6 +182,68 @@ def get_connection_addr(self):
165182
166183 return self ._connection_addr ['host' ], self ._connection_addr ['port' ]
167184
185+ def reset_hba (self ):
186+ """Remove all records from pg_hba.conf."""
187+ status = self .get_status ()
188+ if status == 'not-initialized' :
189+ raise ClusterError (
190+ 'cannot modify HBA records: cluster is not initialized' )
191+
192+ pg_hba = os .path .join (self ._data_dir , 'pg_hba.conf' )
193+
194+ try :
195+ with open (pg_hba , 'w' ):
196+ pass
197+ except IOError as e :
198+ raise ClusterError (
199+ 'cannot modify HBA records: {}' .format (e )) from e
200+
201+ def add_hba_entry (self , * , type = 'host' , database , user , address = None ,
202+ auth_method , auth_options = None ):
203+ """Add a record to pg_hba.conf."""
204+ status = self .get_status ()
205+ if status == 'not-initialized' :
206+ raise ClusterError (
207+ 'cannot modify HBA records: cluster is not initialized' )
208+
209+ if type not in {'local' , 'host' , 'hostssl' , 'hostnossl' }:
210+ raise ValueError ('invalid HBA record type: {!r}' .format (type ))
211+
212+ pg_hba = os .path .join (self ._data_dir , 'pg_hba.conf' )
213+
214+ record = '{} {} {}' .format (type , database , user )
215+
216+ if type != 'local' :
217+ if address is None :
218+ raise ValueError (
219+ '{!r} entry requires a valid address' .format (type ))
220+ else :
221+ record += ' {}' .format (address )
222+
223+ record += ' {}' .format (auth_method )
224+
225+ if auth_options is not None :
226+ record += ' ' + ' ' .join (
227+ '{}={}' .format (k , v ) for k , v in auth_options )
228+
229+ try :
230+ with open (pg_hba , 'a' ) as f :
231+ print (record , file = f )
232+ except IOError as e :
233+ raise ClusterError (
234+ 'cannot modify HBA records: {}' .format (e )) from e
235+
236+ def trust_local_connections (self ):
237+ self .reset_hba ()
238+ self .add_hba_entry (type = 'local' , database = 'all' ,
239+ user = 'all' , auth_method = 'trust' )
240+ self .add_hba_entry (type = 'host' , address = '127.0.0.1/32' ,
241+ database = 'all' , user = 'all' ,
242+ auth_method = 'trust' )
243+ status = self .get_status ()
244+ if status == 'running' :
245+ self .reload ()
246+
168247 def _init_env (self ):
169248 self ._pg_config = self ._find_pg_config (self ._pg_config_path )
170249 self ._pg_config_data = self ._run_pg_config (self ._pg_config )
0 commit comments