def install(self, package): # the installation must be running as the db user (checked in validate_pre_install()) p = self.ctx.props r = self.ctx.r if get_platform().startswith("linux"): self.ctx.logger.info("Disabling startup of default postgres instance") r(sudo_run_program, ["/usr/sbin/update-rc.d", "postgresql", "disable"], cwd="/etc/init.d") r(sudo_run_program, ["/etc/init.d/postgresql", "stop"], cwd="/etc/init.d") # we need to make sure the lock directory exists and is # writable by our postgres user if not os.path.exists("/var/run/postgresql"): self.ctx.r_su(mkdir, "/var/run/postgresql") r(sudo_run_program, ["/bin/chown", p.output_ports.postgres_inst.user, "/var/run/postgresql"], cwd="/") r(mkdir, p.config_port.database_dir) r( run_program, [p.input_ports.postgres.initdb_exe, "-D", p.config_port.database_dir], cwd=p.config_port.database_dir, ) config_file = os.path.join(p.config_port.database_dir, "postgresql.conf") r(check_file_exists, config_file) r( subst_in_file_and_check_count, config_file, [ ( "^" + re.escape("#external_pid_file = ''"), "external_pid_file = '%s'" % p.output_ports.postgres_inst.pid_file, ) ], 1, )
def start(self): p = self.ctx.props r = self.ctx.r if get_platform().startswith("linux"): # we need to make sure the lock directory exists and is # writable by our postgres user if not os.path.exists("/var/run/postgresql"): self.ctx.r_su(mkdir, "/var/run/postgresql") r(sudo_run_program, ["/bin/chown", p.output_ports.postgres_inst.user, "/var/run/postgresql"], cwd="/") self.ctx.r( start_server_as_user, p.output_ports.postgres_inst.user, [p.input_ports.postgres.pg_ctl_exe, "-D", p.config_port.database_dir, "start"], os.path.join(p.input_ports.host.log_directory, "postgres.log"), cwd=p.config_port.database_dir, ) self.ctx.check_poll(12, 5.0, lambda v: v, get_server_status, p.output_ports.postgres_inst.pid_file)