diff --git a/agents/plugins/mk_postgres.py b/agents/plugins/mk_postgres.py index 556186a255e..3fa1db4ca55 100755 --- a/agents/plugins/mk_postgres.py +++ b/agents/plugins/mk_postgres.py @@ -21,9 +21,13 @@ Example of an environment file: -----/home/postgres/db1.env----------------------------------------- -export PGDATABASE="data" -export PGPORT="5432" -export PGVERSION="14" +PGDATABASE="data" +PGPORT="5432" +PGVERSION="14" +# optional: +# PGHOST="hostname.my.domain" +# or (for access through socket): +# PGHOST="/tmp" ---------------------------------------------------------- Inside of the environment file, only `PGPORT` is mandatory. @@ -44,8 +48,8 @@ ---------------------------------------------------------- -----/home/postgres/does-not-exist.env-------------------- -export PGDATABASE="main" -export PGPORT="5432" +PGDATABASE="main" +PGPORT="5432" ---------------------------------------------------------- The only difference being `/home/postgres/does-not-exist.env` does not exist in the first setup. @@ -167,6 +171,7 @@ def __init__(self, db_user, pg_binary_path, instance, process_match_patterns): self.name = instance["name"] self.pg_user = instance["pg_user"] self.pg_port = instance["pg_port"] + self.pg_host = instance["pg_host"] self.pg_database = instance["pg_database"] self.pg_passfile = instance.get("pg_passfile", "") self.pg_version = instance.get("pg_version") @@ -463,6 +468,8 @@ def run_sql_as_db_user( extra_args += " -U %s" % self.pg_user extra_args += " -d %s" % self.pg_database extra_args += " -p %s" % self.pg_port + if self.pg_host != "": + extra_args += " -h %s" % self.pg_host if quiet: extra_args += " -q" @@ -793,6 +800,8 @@ def _run_sql_as_db_user( extra_args += " -U %s" % self.pg_user extra_args += " -d %s" % self.pg_database extra_args += " -p %s" % self.pg_port + if self.pg_host != "": + extra_args += " -h %s" % self.pg_host if quiet: extra_args += " -q" @@ -1206,10 +1215,11 @@ def open_env_file(file_to_open): def parse_env_file(env_file): - # type: (str) -> tuple[str, str, str | None] + # type: (str) -> tuple[str, str, str | None, str] pg_port = None # mandatory in env_file pg_database = "postgres" # default value pg_version = None + pg_host = "" for line in open_env_file(env_file): line = line.strip() @@ -1221,10 +1231,12 @@ def parse_env_file(env_file): pg_port = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip() elif "PGVERSION=" in line: pg_version = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip() + elif "PGHOST=" in line: + pg_host = re.sub(re.compile("#.*"), "", line.split("=")[-1]).strip() if pg_port is None: raise ValueError("PGPORT is not specified in %s" % env_file) - return pg_database, pg_port, pg_version + return pg_database, pg_port, pg_version, pg_host def _parse_INSTANCE_value(value, config_separator): @@ -1261,7 +1273,7 @@ def parse_postgres_cfg(postgres_cfg, config_separator): env_file, pg_user, pg_passfile, instance_name = _parse_INSTANCE_value( value, config_separator ) - pg_database, pg_port, pg_version = parse_env_file(env_file) + pg_database, pg_port, pg_version, pg_host = parse_env_file(env_file) instances.append( { "name": instance_name.strip(), @@ -1269,6 +1281,7 @@ def parse_postgres_cfg(postgres_cfg, config_separator): "pg_passfile": pg_passfile.strip(), "pg_database": pg_database, "pg_port": pg_port, + "pg_host": pg_host, "pg_version": pg_version, } ) @@ -1329,6 +1342,7 @@ def main(argv=None): "pg_user": "postgres", "pg_database": "postgres", "pg_port": "5432", + "pg_host": "", # Assumption: if no pg_passfile is specified no password will be required. # If a password is required but no pg_passfile is specified the process will # interactivly prompt for a password.