Add-Database-PSQL/readme.md
2025-11-14 10:26:18 +00:00

107 lines
3.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Script creazione database cliente PostgreSQL
Questo script Bash automatizza la creazione di un nuovo database PostgreSQL per un cliente, comprensivo di:
- **Tablespace dedicata** con nome cliente in MAIUSCOLO
- **Utente PostgreSQL** dedicato con nome cliente in minuscolo
- **Database** con prefisso di prodotto (es. `hr_nomecliente`)
- **Permessi e privilegi** configurati in modo coerente con gli ambienti Zucchetti
- **Registrazione credenziali** e parametri di connessione nel file `/root/db.list`
---
## Requisiti
- Sistema Linux con:
- Bash
- PostgreSQL 16 installato e funzionante
- Comando `psql` disponibile nel `PATH`
- Cluster PostgreSQL che accetta connessioni:
- host: `localhost`
- utente: `postgres`
- password: `postgres`
(modificabile nello script se necessario)
- Locale `it_IT.UTF-8` installato sul sistema (usato per `LC_COLLATE` e `LC_CTYPE`)
- Directory delle tablespace disponibile:
- `/zucchetti/infinity/PostgreSQL/16/data/`
- Lo script **deve essere eseguito come root** (per poter gestire `chown`/`chmod` sulla directory della tablespace e scrivere in `/root/db.list`).
---
## Cosa fa lo script
Per ogni esecuzione, lo script:
1. **Chiede il nome del cliente**
- Esempio input: `VillaMafalda`
- Internamente:
- `CLIENT_UPPER``VILLAMAFALDA` (per tablespace e directory)
- `CLIENT_LOWER``villamafalda` (per utente database)
2. **Chiede il prodotto** per cui si installa il database:
- Opzioni:
- `AGO`
- `HR`
- `AHRW`
- `AHI`
- `ERP`
- `ALTRO` (prefisso personalizzato)
- Il prodotto viene usato come **prefisso del database** (in minuscolo).
Esempi:
- Prodotto `HR`, cliente `VillaMafalda` → database `hr_villamafalda`
- Prodotto `ALTRO` con prefisso `crm`, cliente `Acme` → database `crm_acme`
3. **Chiede una password** per lutente:
- Se inserita → viene usata così comè (tranne apice `'` che non è ammesso).
- Se lasciata vuota → viene **generata automaticamente**:
- 16 caratteri alfanumerici `[A-Za-z0-9]`
- nessun carattere speciale
- In entrambi i casi la password viene mostrata a video e salvata in `/root/db.list`.
4. **Calcola nomi e percorsi**:
- Tablespace:
- Nome: `VILLAMAFALDA` (sempre MAIUSCOLO)
- Directory: `/zucchetti/infinity/PostgreSQL/16/data/VILLAMAFALDA`
- Utente DB:
- Nome: `villamafalda` (sempre minuscolo)
- Database:
- Nome: ad es. `hr_villamafalda`
5. **Crea la directory della tablespace** (se non esiste):
- `mkdir -p /zucchetti/infinity/PostgreSQL/16/data/VILLAMAFALDA`
- `chown postgres:postgres`
- `chmod 700`
6. **Si collega a PostgreSQL come utente `postgres`** (password `postgres`) e:
- Crea/aggiorna il **ruolo utente** del cliente con:
- `LOGIN SUPERUSER CREATEDB CREATEROLE INHERIT BYPASSRLS`
- Crea/aggiorna la **tablespace**:
- `CREATE TABLESPACE VILLAMAFALDA OWNER villamafalda LOCATION '...'`
- Se già esiste → `ALTER TABLESPACE ... OWNER TO villamafalda`
- Crea/aggiorna il **database**:
- `CREATE DATABASE <db_name> OWNER <user> TABLESPACE <ts>`
- `ENCODING 'UTF8' LC_COLLATE 'it_IT.UTF-8' LC_CTYPE 'it_IT.UTF-8'`
- Se già esiste → `ALTER DATABASE ... OWNER TO <user>`
- Imposta i **privilegi**:
- `REVOKE ALL ON DATABASE ... FROM utente`
- `GRANT ALL PRIVILEGES ON DATABASE ... TO utente`
- `REVOKE ALL ON TABLESPACE ... FROM utente`
- `GRANT CREATE ON TABLESPACE ... TO utente WITH GRANT OPTION`
- In pgAdmin vedrai:
- Nel database: privilegi tipo `CTc` per lutente
- Nella tablespace: privilegi tipo `C*` per lutente, con grantor `postgres`
7. **Aggiorna il file `/root/db.list`**
Per ogni nuova creazione aggiunge un blocco con il seguente formato:
```text
Cliente: <utente DB>
Host: <hostname FQDN del server>
IP: <primo IP IPv4>
User: <utente DB>
Password: <password dell'utente DB>
Database: <nome database>
Tablespace: <directory tablespace>