@@ -125,21 +125,38 @@ done
125125
126126# Database
127127say " " ; info " PostgreSQL"
128- DB_MODE=" $( ask ' Provision a local database for me? (yes) or use an existing DATABASE_URL (no)' ' yes' ) "
129- DATABASE_URL=' ' ; CREATE_DB=false
128+ say " 1) Project-local DB — runs inside the project on a random free port, supervised"
129+ say " by PM2. Best when the usual ports are already taken."
130+ say " 2) System PostgreSQL — create a database/user on the host's PostgreSQL service."
131+ say " 3) Existing DATABASE_URL — point at a database you already have."
132+ DB_CHOICE=" $( ask ' Choose 1, 2 or 3' ' 1' ) "
133+ DATABASE_URL=' ' ; DB_PROVISION=' local'
130134DB_NAME=' opencoperlock' ; DB_USER=' opencoperlock' ; DB_PASS=' '
131- if [[ " $DB_MODE " =~ ^[Yy] ]]; then
132- CREATE_DB=true
133- DB_NAME=" $( ask ' Database name' ' opencoperlock' ) "
134- DB_USER=" $( ask ' Database user' ' opencoperlock' ) "
135- # Restrict identifiers to a safe character set (they go into SQL).
136- [[ " $DB_NAME " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || die " Invalid database name (use letters, digits, underscore)."
137- [[ " $DB_USER " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || die " Invalid database user (use letters, digits, underscore)."
138- DB_PASS=" $( openssl rand -hex 16) "
139- DATABASE_URL=" postgresql://${DB_USER} :${DB_PASS} @localhost:5432/${DB_NAME} "
140- else
141- while [[ -z " $DATABASE_URL " ]]; do DATABASE_URL=" $( ask ' Existing DATABASE_URL' ) " ; done
142- fi
135+ case " $DB_CHOICE " in
136+ 1)
137+ DB_PROVISION=' local'
138+ DB_NAME=" $( ask ' Database name' ' opencoperlock' ) "
139+ DB_USER=" $( ask ' Database user' ' opencoperlock' ) "
140+ [[ " $DB_NAME " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || die " Invalid database name (letters, digits, underscore)."
141+ [[ " $DB_USER " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || die " Invalid database user (letters, digits, underscore)."
142+ DB_PASS=" $( openssl rand -hex 16) "
143+ # The port (and thus DATABASE_URL) is chosen at provisioning time (Step 3).
144+ ;;
145+ 2)
146+ DB_PROVISION=' system'
147+ DB_NAME=" $( ask ' Database name' ' opencoperlock' ) "
148+ DB_USER=" $( ask ' Database user' ' opencoperlock' ) "
149+ [[ " $DB_NAME " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || die " Invalid database name (letters, digits, underscore)."
150+ [[ " $DB_USER " =~ ^[A-Za-z_][A-Za-z0-9_]* $ ]] || die " Invalid database user (letters, digits, underscore)."
151+ DB_PASS=" $( openssl rand -hex 16) "
152+ DATABASE_URL=" postgresql://${DB_USER} :${DB_PASS} @localhost:5432/${DB_NAME} "
153+ ;;
154+ 3)
155+ DB_PROVISION=' existing'
156+ while [[ -z " $DATABASE_URL " ]]; do DATABASE_URL=" $( ask ' Existing DATABASE_URL' ) " ; done
157+ ;;
158+ * ) die " Invalid choice: $DB_CHOICE " ;;
159+ esac
143160
144161# Storage
145162say " " ; info " Storage & limits"
@@ -177,7 +194,7 @@ say " Topology : $([[ $TOPO == 1 ]] && echo 'two subdomains' || { [[
177194say " API URL (browser) : ${NEXT_PUBLIC_API_URL} "
178195say " Web / API ports : ${WEB_PORT} / ${API_PORT} "
179196say " Admin email : ${ADMIN_EMAIL} "
180- say " Database : $( [[ $CREATE_DB == true ]] && echo " create local '${DB_NAME} '" || echo ' existing (provided URL)' ) "
197+ say " Database : $( case $DB_PROVISION in local ) echo " project-local ' ${DB_NAME} ' (random port, PM2) " ;; system) echo " system PostgreSQL ' ${DB_NAME}' " ;; *) echo 'existing (provided URL)';; esac )"
181198say " Storage path : ${STORAGE_PATH} "
182199say " Per-user quota : ${QUOTA_GB} GiB Global cap: ${CAP_GB} GiB"
183200say " ClamAV / VirusTotal : $( [[ $CLAMAV_ENABLED == true ]] && echo on || echo off) / $( [[ -n $VIRUSTOTAL_API_KEY ]] && echo set || echo off) "
@@ -228,11 +245,14 @@ if $CLAMAV_ENABLED && ! command -v clamdscan >/dev/null 2>&1; then
228245 fi
229246fi
230247
231- # PostgreSQL
232- if $CREATE_DB && ! command -v psql > /dev/null 2>&1 ; then
233- if [[ " $PKG " == apt ]] && ask_yn ' PostgreSQL not found. Install it?' ' Y' ; then
248+ # PostgreSQL binaries (needed for system provisioning and for a project-local cluster).
249+ if [[ " $DB_PROVISION " != existing ]] && ! command -v initdb > /dev/null 2>&1 \
250+ && ! ls /usr/lib/postgresql/* /bin/initdb > /dev/null 2>&1 ; then
251+ if [[ " $PKG " == apt ]] && ask_yn ' PostgreSQL is not installed. Install it?' ' Y' ; then
234252 apt_install postgresql postgresql-contrib
235- $SUDO systemctl enable --now postgresql
253+ # The project-local cluster does not use the system service; stop it to free port 5432
254+ # if the operator wants, but leave it as-is by default.
255+ [[ " $DB_PROVISION " == system ]] && $SUDO systemctl enable --now postgresql || true
236256 fi
237257fi
238258
@@ -243,11 +263,18 @@ if $SETUP_TLS && ! command -v certbot >/dev/null 2>&1 && [[ "$PKG" == apt ]]; th
243263# ─────────────────────────────────────────────────────────────────────────────
244264# 3. DATABASE PROVISIONING
245265# ─────────────────────────────────────────────────────────────────────────────
246- if $CREATE_DB ; then
247- say " " ; info " Step 3 / 8 — Creating PostgreSQL database & user"
248- need_sudo
249- # Create role if missing, set password, create db owned by it. Idempotent.
250- $SUDO -u postgres psql -v ON_ERROR_STOP=1 << SQL || die 'Database provisioning failed.'
266+ case " $DB_PROVISION " in
267+ local)
268+ say " " ; info " Step 3 / 8 — Creating a project-local PostgreSQL (random port)"
269+ # postgres-local.sh must run as the unprivileged app user; it prints the DATABASE_URL.
270+ DATABASE_URL=" $( DB_NAME=" $DB_NAME " DB_USER=" $DB_USER " DB_PASS=" $DB_PASS " ./scripts/postgres-local.sh init) "
271+ [[ -n " $DATABASE_URL " ]] || die ' Project-local database provisioning failed.'
272+ ok " Project-local PostgreSQL ready (${DATABASE_URL##*@ } )"
273+ ;;
274+ system)
275+ say " " ; info " Step 3 / 8 — Creating PostgreSQL database & user on the system service"
276+ need_sudo
277+ $SUDO -u postgres psql -v ON_ERROR_STOP=1 << SQL || die 'Database provisioning failed.'
251278DO \$\$ BEGIN
252279 IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '${DB_USER} ') THEN
253280 CREATE ROLE "${DB_USER} " LOGIN PASSWORD '${DB_PASS} ';
@@ -258,10 +285,12 @@ END \$\$;
258285SELECT 'CREATE DATABASE "${DB_NAME} " OWNER "${DB_USER} "'
259286WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = '${DB_NAME} ')\gexec
260287SQL
261- ok " Database '${DB_NAME} ' ready"
262- else
263- say " " ; info " Step 3 / 8 — Using existing database (skipped provisioning)"
264- fi
288+ ok " Database '${DB_NAME} ' ready"
289+ ;;
290+ * )
291+ say " " ; info " Step 3 / 8 — Using existing database (skipped provisioning)"
292+ ;;
293+ esac
265294
266295# ─────────────────────────────────────────────────────────────────────────────
267296# 4. STORAGE DIRECTORIES
0 commit comments