#!/bin/bash # Script per setup PostgreSQL in container Incus set -e CONTAINER_NAME=${1:-pg-server} POSTGRES_PASSWORD=${2:-postgres} echo "Creating Incus container: $CONTAINER_NAME" # Creare container incus launch images:ubuntu/22.04 "$CONTAINER_NAME" --wait echo "Installing PostgreSQL..." # Installare PostgreSQL incus exec "$CONTAINER_NAME" -- apt update incus exec "$CONTAINER_NAME" -- apt install -y postgresql postgresql-contrib echo "Starting PostgreSQL..." # Avviare PostgreSQL incus exec "$CONTAINER_NAME" -- systemctl start postgresql incus exec "$CONTAINER_NAME" -- systemctl enable postgresql # Impostare password postgres incus exec "$CONTAINER_NAME" -- sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '$POSTGRES_PASSWORD';" # Permettere connessioni TCP incus exec "$CONTAINER_NAME" -- bash -c " echo \"host all all 0.0.0.0/0 md5\" >> /etc/postgresql/14/main/pg_hba.conf sed -i \"s/#listen_addresses = 'localhost'/listen_addresses = '*'/\" /etc/postgresql/14/main/postgresql.conf " # Riavviare PostgreSQL incus exec "$CONTAINER_NAME" -- systemctl restart postgresql # Ottenere IP IP=$(incus list "$CONTAINER_NAME" -c4 | tail -n1 | awk '{print $1}') echo "" echo "✓ PostgreSQL is running!" echo "" echo "Connection details:" echo " Host: $IP" echo " Port: 5432" echo " User: postgres" echo " Password: $POSTGRES_PASSWORD" echo "" echo "Update .env file with:" echo " POSTGRES_HOST=$IP" echo " POSTGRES_PASSWORD=$POSTGRES_PASSWORD"