2018年7月16日月曜日

VagrantでpgAdmin4とPostgreSQL10をインストールした仮想マシン(Debian Stretch/9.4)を構築する

pgAdmin4はPostgreSQL用のwebインターフェイスです。

〇pgAdmin4の画面

ユーザはadmin@localhost、パスワードにはadminpassを指定します。
接続情報として、DBとしてdwh、ユーザ名dwh、パスワードdwhを指定します。

〇構築方法
以下のVagrantfileを使用して、pgAdmin4とPostgreSQL10をインストールした仮想マシン(Debian Stretch/9.4)を構築する事ができます。

Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "bento/debian-9.4"
  config.vm.hostname = "db94pg10pgadmin4"
  config.vm.provider :virtualbox do |vbox|
     vbox.name = "db94pg10pgadmin4"
     vbox.cpus = 2
     vbox.memory = 2048
     vbox.customize ["modifyvm", :id, "--nicpromisc2","allow-all"]
  end
config.vm.network "private_network", ip: "192.168.55.105", :netmask => "255.255.255.0"
config.vm.network "public_network", ip:"192.168.1.105", :netmask => "255.255.255.0"
  config.vm.provision "shell", inline: <<-SHELL
apt-get -y install task-japanese
sed -i -e 's/# ja_JP.UTF-8 UTF-8/ja_JP.UTF-8 UTF-8/' /etc/locale.gen
locale-gen
update-locale LANG=ja_JP.UTF-8
localectl set-locale LANG=ja_JP.UTF-8
localectl set-keymap jp106
apt-get update
#DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade


# install postgresql
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
apt-get -y install wget ca-certificates
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
apt-get update
apt-get upgrade
apt-get -y install postgresql-10 

echo "listen_addresses='*'" >> /etc/postgresql/10/main/postgresql.conf

#sed -i 's/host.*all.*all.*127.0.0.1/#host    all             all             127.0.0.1/g' /etc/postgresql/10/main/pg_hba.conf

echo "host    all         all         127.0.0.1/32          password" >> /etc/postgresql/10/main/pg_hba.conf
echo "host    all         all         192.168.1.0/24          password" >> /etc/postgresql/10/main/pg_hba.conf
echo "host    all         all         192.168.55.0/24          password" >> /etc/postgresql/10/main/pg_hba.conf

su - postgres << EOF
createdb -T template0 --locale=ja_JP.UTF-8 --encoding=UTF8 dwh
psql -c "
alter user postgres with password 'postgres';
create user dwh with password 'dwh';
grant all privileges on database dwh to dwh;
"
EOF
echo "postgres:postgres" | chpasswd
systemctl restart postgresql.service


# install pgadmin4
apt-get -y install libpq-dev python-dev python-pip libapache2-mod-wsgi apache2
wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v3.0/pip/pgadmin4-3.0-py2.py3-none-any.whl
pip install pgadmin4-3.0-py2.py3-none-any.whl

cat << EOF >> /usr/local/lib/python2.7/dist-packages/pgadmin4/config_distro.py
LOG_FILE = '/var/log/pgadmin4/pgadmin4.log'
SQLITE_PATH = '/usr/local/lib/python2.7/dist-packages/pgadmin4/pgadmin4.db'
SESSION_DB_PATH = '/usr/local/lib/python2.7/dist-packages/pgadmin4/sessions'
STORAGE_DIR = '/usr/local/lib/python2.7/dist-packages/pgadmin4/storage'
EOF

cd /usr/local/lib/python2.7/dist-packages/pgadmin4
cat << EOF | python setup.py
admin@localhost
adminpass
adminpass
EOF
mkdir -p /var/log/pgadmin4
chown -R www-data:www-data /var/log/pgadmin4
mkdir -p /usr/local/lib/python2.7/dist-packages/pgadmin4/storage
chown -R www-data:www-data /usr/local/lib/python2.7/dist-packages/pgadmin4


cat << EOF > /etc/apache2/sites-available/pgadmin4.conf
<VirtualHost *>
  ServerName db94pg10pgadmin4

  WSGIDaemonProcess pgadmin processes=1 threads=25
  WSGIScriptAlias / /usr/local/lib/python2.7/dist-packages/pgadmin4/pgAdmin4.wsgi
  <Directory "/usr/local/lib/python2.7/dist-packages/pgadmin4/">
    WSGIProcessGroup pgadmin
    WSGIApplicationGroup %{GLOBAL}
    Require all granted
  </Directory>
</VirtualHost>
EOF

a2enmod wsgi
a2ensite pgadmin4.conf
a2dissite 000-default.conf
systemctl restart apache2

echo 'access to http://192.168.55.105/'


SHELL
end

0 件のコメント:

コメントを投稿