【Vagrant】 Build PG15 on rhel8

发布时间 2023-10-07 21:59:41作者: DBAGPT

vagrant创建一套single PG15,同时创建一个新的数据库db_pg15 (密同)

Vagrant.configure("2") do |config|
    vms = [
#      { name: "app01", box: "centos-8", hostname: "app01", ip: "192.168.33.11" },
#      { name: "gitlab", box: "centos-8", hostname: "gitlab", ip: "192.168.33.12" },
#      { name: "db01", box: "centos-8", hostname: "db01", ip: "192.168.56.11" },
      { name: "db02", box: "centos-8", hostname: "db02", ip: "192.168.56.12", provision: "postgres_install.sh" }
    ]
  
    vms.each do |vm_data|
      config.vm.define vm_data[:name] do |vm|
        vm.vm.box = vm_data[:box]
        vm.vm.hostname = vm_data[:hostname]
        vm.vm.network "private_network", type: "static", ip: vm_data[:ip]
  
        vm.vm.provider "virtualbox" do |vb|
          vb.name = vm_data[:name]
          vb.memory = 1024
          vb.cpus = 2
        end
  
        if vm_data[:provision]
          vm.vm.provision "shell", path: vm_data[:provision]
        end
      end
    end
  end

postgres_install.sh

#!/bin/bash
sudo  dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm

sudo dnf update -y
sudo dnf -y module disable postgresql


sudo dnf -y install postgresql15-server


mkdir -p /pgsql/15/{data,dump,dba,backup}
chown -R postgres:postgres /pgsql
chmod -R 700 /pgsql 
echo "export PATH=/usr/pgsql-15/bin:$PATH">> /etc/profile
 
echo "export PGHOME=/pgsql/15" >> /var/lib/pgsql/.bash_profile
echo "export PGDATA=/pgsql/15/data " >> /var/lib/pgsql/.bash_profile
echo "export PGDUMP=/pgsql/15/dump  " >> /var/lib/pgsql/.bash_profile
echo "export PGBACKUP=/pgsql/15/backup " >> /var/lib/pgsql/.bash_profile

sed -e 's|Environment=PGDATA=/var/lib/pgsql/15/data/|Environment=PGDATA=/pgsql/15/data/|g' -i.bak /usr/lib/systemd/system/postgresql-15.service



sudo /usr/pgsql-15/bin/postgresql-15-setup initdb

echo "host    postgres    postgres    0.0.0.0/0   scram-sha-256" | sudo tee -a /pgsql/15/data/pg_hba.conf > /dev/null

sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" -i.bak /pgsql/15/data/postgresql.conf

sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
sudo systemctl status postgresql-15


sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
/bin/systemctl restart sshd

echo postgres | passwd postgres --stdin 

echo root | passwd root --stdin 




# reset the password for postgres
echo "Setting password for postgres user"
cd /
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"

# create new DB named db_pg15
echo "Creating a new PostgreSQL database"
sudo -u postgres createdb main_db

# reset the password in db_pg15
echo "Creating a new PostgreSQL user and granting access to the database"
sudo -u postgres psql -c "CREATE USER db_pg15 WITH PASSWORD 'db_pg15';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE db_pg15 TO db_pg15;"

#!/bin/bash

# export the db name
#DB_NAME="db_pg15"

# export the db user
#DB_USER="db_user"

# expose the ip
#ALLOWED_NETWORK="192.168.33.0/24"

# export the password 
#DB_PASSWORD="db_user"

# update PostgreSQL pg_hba.conf
echo "host    db_pg15    db_pg15    0.0.0.0/0   scram-sha-256" | sudo tee -a /pgsql/15/data/pg_hba.conf > /dev/null

# restart PostgreSQL
sudo systemctl restart postgresql-15