-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathinstall-postgresql.sh
More file actions
executable file
·41 lines (34 loc) · 1.36 KB
/
install-postgresql.sh
File metadata and controls
executable file
·41 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash
# Install PostgreSQL
echo "Installing PostgreSQL..."
yay -S --noconfirm --needed postgresql
# Check if data directory already exists and is initialized
if [ ! -d "/var/lib/postgres/data" ] || [ -z "$(ls -A /var/lib/postgres/data 2>/dev/null)" ]; then
echo "Initializing PostgreSQL database..."
sudo -u postgres initdb -D /var/lib/postgres/data --locale=C.UTF-8 --encoding=UTF8 --data-checksums
else
echo "PostgreSQL data directory already initialized, skipping..."
fi
# Start and enable PostgreSQL service
echo "Starting PostgreSQL service..."
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Wait for PostgreSQL to be ready
sleep 2
# Create a database user matching the current user if it doesn't exist
echo "Setting up PostgreSQL user..."
if ! sudo -u postgres psql -tAc "SELECT 1 FROM pg_user WHERE usename='$USER'" | grep -q 1; then
sudo -u postgres createuser --interactive -d "$USER"
echo "Created PostgreSQL user: $USER"
else
echo "PostgreSQL user $USER already exists"
fi
# Create a default database for the user if it doesn't exist
if ! sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -qw "$USER"; then
createdb "$USER"
echo "Created default database: $USER"
else
echo "Database $USER already exists"
fi
echo "PostgreSQL installation and setup complete!"
echo "You can now connect to PostgreSQL using: psql"