Edit File: installpostgres
#!/usr/local/cpanel/3rdparty/bin/perl # cpanel - installpostgres Copyright 2015 cPanel, Inc. # All rights Reserved. # copyright@cpanel.net http://cpanel.net # This code is subject to the cpanel license. Unauthorized copying is prohibited package scripts::installpostgres; use strict; use Cpanel::SafeRun::Simple (); use Cpanel::Sys::OS::Check (); use Cpanel::SysPkgs (); use Whostmgr::Postgres (); run(@ARGV) unless caller(); sub run { my @args = @_; if ( -d "/root/.cpupdates" ) { system("rm -f /root/.cpupdates/*"); } my $minversion = Cpanel::Sys::OS::Check::has_systemd() ? q{9.2.x} : q{7.4.x}; my $restart_instruction; if ( Cpanel::Sys::OS::Check::has_systemd() ) { $restart_instruction = "\tsystemctl restart postgresql"; } else { $restart_instruction = "\t/sbin/service postgresql stop\n" . "\t/sbin/service postgresql start\n"; } my $notice = <<"EOS"; This script installs PostgreSQL $minversion or later. If your system runs an older version of PostgreSQL, you must dump your databases to a file, and then restore them after installation. PostgreSQL $minversion is NOT backwards compatible. If no PostgreSQL databases exist on your system, run the following command to force the creation of a PostgreSQL $minversion -style setup: \tmv /var/lib/pgsql /var/lib/pgsql.old $restart_instruction Do not run this command if databases exist that you wish to keep! Are you certain that you wish to proceed? [y/(n)]: EOS chomp $notice; print $notice; if ( $args[0] !~ /\-\-yes/ ) { my $res = <STDIN>; if ( $res !~ /^y/i ) { print "Exiting...\n"; exit(); } } my $syspkgobj = Cpanel::SysPkgs->new(); if ( !$syspkgobj ) { die print "The system could not create the SysPkgs object.\n"; } my @pkgs = ( "rh-postgresql", "rh-postgresql-devel", "rh-postgresql-libs", "rh-postgresql-server", "postgresql", "postgresql-devel", "postgresql-libs", "postgresql-server" ); $syspkgobj->install( 'pkglist' => \@pkgs ); if ( Cpanel::Sys::OS::Check::has_systemd() ) { if ( !-e '/var/lib/pgsql/data/global' ) { Cpanel::SafeRun::Simple::saferunnoerror( '/usr/bin/postgresql-setup', 'initdb' ); } Cpanel::SafeRun::Simple::saferunnoerror( '/usr/local/cpanel/bin/setpostgresconfig', '--force' ); } else { if ( !-e "/etc/rc.d/init.d/postgresql" && -e "/etc/rc.d/init.d/rhdb" ) { symlink( "rhdb", "/etc/rc.d/init.d/postgresql" ); } if ( !-e '/var/lib/pgsql/data/global' ) { Cpanel::SafeRun::Simple::saferunnoerror(qw{/sbin/service postgresql initdb}); Cpanel::SafeRun::Simple::saferunnoerror( '/usr/local/cpanel/bin/setpostgresconfig', '--force' ); } } Cpanel::SafeRun::Simple::saferun( '/usr/local/cpanel/bin/updatephppgadmin', '--force' ); Cpanel::SafeRun::Simple::saferun( '/usr/local/cpanel/scripts/cpservice', 'postgresql', 'enable', '35' ); my ( $status, $message ); ( $status, $message ) = Whostmgr::Postgres::update_config(); print $message. "\n"; Cpanel::SafeRun::Simple::saferunnoerror('/usr/local/cpanel/scripts/restartsrv_postgres'); my $install_ok = $? == 0 ? 1 : 0; if ($install_ok) { ( $status, $message ) = Whostmgr::Postgres::create_dbowners_for_cpusers(); print $message. "\n"; $install_ok &&= $status; if ($install_ok) { print q{ The PostgreSQL packages successfully installed. To configure PostgreSQL, set your password, and enable PostgreSQL for user accounts, navigate to WHM's Configure PostgreSQL interface (Home >> SQL Services >> Configure PostgreSQL). }; print "Setting a random password for the PostgreSQL database user.\n"; Cpanel::SafeRun::Simple::saferun( '/usr/local/cpanel/bin/postgrescheck', '--check-auth', '--reset-pass-on-fail' ); Cpanel::SafeRun::Simple::saferun('/usr/local/cpanel/bin/cpsessetup'); } } # install_ok value can change in the previous block if ( !$install_ok ) { print STDERR qq{The PostgreSQL installation failed.\n}; } system '/usr/local/cpanel/bin/build_global_cache'; # reset haspostgres exit( $install_ok ? 0 : 1 ); } 1;