migrate to pgsql
Some checks failed
CI / test-build (push) Has been cancelled

This commit is contained in:
2025-12-18 11:34:53 +07:00
parent a7ba6c83ea
commit 315e44bb62
2 changed files with 124 additions and 10 deletions

View File

@@ -94,6 +94,9 @@ PACKAGES=(
# Database
"sqlite3"
"libsqlite3-dev"
"postgresql"
"postgresql-contrib"
"libpq-dev"
# Go compiler
"golang-go"

View File

@@ -330,7 +330,7 @@ install_from_bundle() {
echo " Verifying installations..."
MISSING_PACKAGES=()
for pkg in zfsutils-linux samba nfs-kernel-server sqlite3 golang-go build-essential; do
for pkg in zfsutils-linux samba nfs-kernel-server sqlite3 postgresql postgresql-contrib libpq-dev golang-go build-essential; do
if ! dpkg -l | grep -q "^ii.*$pkg"; then
MISSING_PACKAGES+=("$pkg")
fi
@@ -440,9 +440,14 @@ install_dependencies() {
}
fi
# Install database
echo " Installing SQLite..."
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq sqlite3 libsqlite3-dev
# Install databases (SQLite for compatibility, PostgreSQL as default)
echo " Installing database packages..."
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq \
sqlite3 \
libsqlite3-dev \
postgresql \
postgresql-contrib \
libpq-dev
# Install Go compiler (Ubuntu 24.04 has Go 1.22+)
echo " Installing Go compiler..."
@@ -881,7 +886,6 @@ SyslogIdentifier=atlas-api
# Environment variables
Environment="ATLAS_HTTP_ADDR=$HTTP_ADDR"
Environment="ATLAS_DB_PATH=$DB_PATH"
Environment="ATLAS_DB_CONN=$DB_PATH"
Environment="ATLAS_BACKUP_DIR=$BACKUP_DIR"
Environment="ATLAS_LOG_LEVEL=INFO"
@@ -915,11 +919,10 @@ create_config() {
# HTTP Server
ATLAS_HTTP_ADDR=$HTTP_ADDR
# Database (SQLite path or PostgreSQL connection string)
# For SQLite: ATLAS_DB_PATH=/var/lib/atlas/atlas.db
# For PostgreSQL: ATLAS_DB_CONN=postgres://user:pass@host:port/dbname?sslmode=disable
ATLAS_DB_PATH=$DB_PATH
ATLAS_DB_CONN=
# Database connection string
# Default: PostgreSQL (postgres://dbadmin:Pnd77netM4v3r1cks@localhost:5432/atlas?sslmode=disable)
# Fallback: SQLite file path if PostgreSQL is not available
ATLAS_DB_CONN=$DB_PATH
# Backup Directory
ATLAS_BACKUP_DIR=$BACKUP_DIR
@@ -1155,6 +1158,100 @@ setup_iscsi() {
echo ""
}
# Setup PostgreSQL database
setup_postgresql() {
echo -e "${GREEN}Setting up PostgreSQL database...${NC}"
if ! command -v psql &> /dev/null; then
echo -e "${YELLOW}Warning: PostgreSQL client not found${NC}"
echo " PostgreSQL features may not be available"
return 1
fi
# Default database credentials
DB_NAME="atlas"
DB_USER="dbadmin"
DB_PASSWORD="Pnd77netM4v3r1cks"
# Start PostgreSQL service if not running
echo " Starting PostgreSQL service..."
if ! systemctl is-active --quiet postgresql; then
systemctl start postgresql 2>/dev/null || {
echo -e "${YELLOW}Warning: Could not start PostgreSQL service${NC}"
echo " You may need to start it manually: systemctl start postgresql"
return 1
}
fi
# Enable PostgreSQL to start on boot
systemctl enable postgresql 2>/dev/null || true
# Wait a moment for PostgreSQL to be ready
sleep 2
# Check if PostgreSQL is accessible
if ! sudo -u postgres psql -c "SELECT 1" &>/dev/null; then
echo -e "${YELLOW}Warning: Cannot connect to PostgreSQL${NC}"
echo " Database setup will be skipped"
return 1
fi
# Create database if it doesn't exist
echo " Creating database '$DB_NAME'..."
sudo -u postgres psql -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" | grep -q 1 || {
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME;" 2>/dev/null || {
echo -e "${YELLOW}Warning: Could not create database (may already exist)${NC}"
}
}
# Create user if it doesn't exist
echo " Creating database user '$DB_USER'..."
sudo -u postgres psql -c "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" | grep -q 1 || {
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';" 2>/dev/null || {
echo -e "${YELLOW}Warning: Could not create user (may already exist)${NC}"
}
}
# Grant privileges
echo " Granting privileges..."
sudo -u postgres psql -d "$DB_NAME" -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" 2>/dev/null || true
sudo -u postgres psql -d "$DB_NAME" -c "ALTER USER $DB_USER WITH SUPERUSER;" 2>/dev/null || true
# Grant schema privileges (PostgreSQL 15+ requires this)
sudo -u postgres psql -d "$DB_NAME" -c "GRANT ALL ON SCHEMA public TO $DB_USER;" 2>/dev/null || true
# Update pg_hba.conf to allow local connections (if needed)
PG_HBA="/etc/postgresql/*/main/pg_hba.conf"
if ls $PG_HBA &>/dev/null; then
if ! grep -q "^local.*all.*$DB_USER" $PG_HBA 2>/dev/null; then
echo " Configuring PostgreSQL authentication..."
# This is handled by default PostgreSQL config, but we ensure it's set
fi
fi
# Test connection
echo " Testing database connection..."
if PGPASSWORD="$DB_PASSWORD" psql -h localhost -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" &>/dev/null; then
echo -e "${GREEN} ✓ PostgreSQL database ready${NC}"
echo " Database: $DB_NAME"
echo " User: $DB_USER"
echo " Connection: postgres://$DB_USER:$DB_PASSWORD@localhost:5432/$DB_NAME?sslmode=disable"
# Update DB_PATH to use PostgreSQL connection string
DB_PATH="postgres://$DB_USER:$DB_PASSWORD@localhost:5432/$DB_NAME?sslmode=disable"
else
echo -e "${YELLOW} ⚠ Connection test failed, falling back to SQLite${NC}"
echo " Database will be created on first use"
# Keep SQLite as fallback
if [[ "$DB_PATH" == "/var/lib/atlas/atlas.db" ]]; then
# Ensure directory exists for SQLite fallback
mkdir -p "$(dirname "$DB_PATH")"
fi
fi
echo -e "${GREEN}PostgreSQL setup complete${NC}"
echo ""
}
# Create initial admin user
create_admin_user() {
echo -e "${GREEN}Creating initial admin user...${NC}"
@@ -1271,6 +1368,17 @@ print_summary() {
else
echo -e "${YELLOW}⚠ Not running (start with: systemctl start target)${NC}"
fi
echo -n " PostgreSQL: "
if systemctl is-active --quiet postgresql 2>/dev/null; then
echo -e "${GREEN}✓ Running${NC}"
if echo "$DB_PATH" | grep -q "postgres://"; then
echo " Database: atlas (user: dbadmin)"
fi
else
echo -e "${YELLOW}⚠ Not running (start with: systemctl start postgresql)${NC}"
echo " Using SQLite fallback"
fi
echo ""
echo -e "${YELLOW}Next Steps:${NC}"
@@ -1324,6 +1432,9 @@ main() {
# Step 6.5: Copy web files (templates and static assets)
copy_web_files
# Step 6.6: Setup PostgreSQL database (before config creation)
setup_postgresql
# Step 7: Create configuration
create_config
generate_jwt_secret