diff --git a/postinstall.php b/postinstall.php index ac13e1a..723957a 100644 --- a/postinstall.php +++ b/postinstall.php @@ -9,19 +9,16 @@ function installpostgre() $db_user = 'dragoncore30'; // default user $db_name = 'dragoncore'; // default db name - // If config already exists, load existing password (and user if needed) + // If config already exists, load existing password (and user/db if needed) if (file_exists($configPath)) { include $configPath; // defines $db_pass, $db_user, $db_name, etc. if (isset($db_pass)) { $password = $db_pass; } - if (isset($db_user)) { - $db_user = $db_user; - } - if (isset($db_name)) { - $db_name = $db_name; - } + // if you ever want to override db_user/db_name from config, you can: + // if (isset($db_user)) $db_user = $db_user; + // if (isset($db_name)) $db_name = $db_name; } // If no password loaded from config, generate a new one @@ -36,20 +33,20 @@ function installpostgre() echo 'sudo apt update' . "\n"; echo 'sudo apt install postgresql postgresql-contrib -y' . "\n"; - #$pgVersion = exec('pg_config --version | awk \'{print $NF}\' | cut -d\'.\' -f1-2'); - #echo 'sudo cp /etc/postgresql/' . $pgVersion . '/main/pg_hba.conf /etc/postgresql/' . $pgVersion . '/main/pg_hba.conf.bak' . "\n"; - #echo 'sudo sh -c "echo \'host all all 127.0.0.1/32 md5\' > /etc/postgresql/' . $pgVersion . '/main/pg_hba.conf"' . "\n"; echo 'sudo systemctl restart postgresql' . "\n"; + + // DB + user + basic grants echo 'sudo -u postgres psql -c "CREATE DATABASE ' . $db_name . ';"' . "\n"; echo 'sudo -u postgres psql -c "CREATE USER ' . $db_user . ' WITH PASSWORD \'' . $password . '\';"' . "\n"; echo 'sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE ' . $db_name . ' TO ' . $db_user . ';"' . "\n"; - echo 'sudo -u postgres psql -c "GRANT USAGE, CREATE ON SCHEMA public TO ' . $db_user . ';"' . "\n"; + echo 'sudo -u postgres psql -d ' . $db_name . ' -c "GRANT USAGE, CREATE ON SCHEMA public TO ' . $db_user . ';"' . "\n"; - // *** Change owner of all tables in public to $db_user *** - echo "sudo -u postgres psql -d {$db_name} -c 'DO \$\$ DECLARE r RECORD; BEGIN FOR r IN SELECT tablename FROM pg_tables WHERE schemaname = ''public'' LOOP EXECUTE ''ALTER TABLE public.'' || quote_ident(r.tablename) || '' OWNER TO {$db_user}''; END LOOP; END \$\$;'" . "\n"; + // OPTIONAL: make dragoncore2 the owner of everything that was owned by dragoncore in this DB + echo 'sudo -u postgres psql -d ' . $db_name . ' -c "REASSIGN OWNED BY dragoncore TO ' . $db_user . ';"' . "\n"; - // *** Change owner of all sequences in public to $db_user *** - echo "sudo -u postgres psql -d {$db_name} -c 'DO \$\$ DECLARE r RECORD; BEGIN FOR r IN SELECT sequencename FROM pg_sequences WHERE schemaname = ''public'' LOOP EXECUTE ''ALTER SEQUENCE public.'' || quote_ident(r.sequencename) || '' OWNER TO {$db_user}''; END LOOP; END \$\$;'" . "\n"; + // Also give full privileges on all existing tables/sequences (nice to have) + echo 'sudo -u postgres psql -d ' . $db_name . ' -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ' . $db_user . ';"' . "\n"; + echo 'sudo -u postgres psql -d ' . $db_name . ' -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ' . $db_user . ';"' . "\n"; echo 'sudo systemctl restart postgresql' . "\n"; diff --git a/version.txt b/version.txt index c62d7fb..ab8a90e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -30 Year Of the Tubular Dragon \ No newline at end of file +31 Year Of the Tubular Dragon \ No newline at end of file