LoginSignup
5
9

More than 3 years have passed since last update.

Mac+Vagrant+PostgreSQL環境構築

Last updated at Posted at 2019-04-07

 自分用のメモです。

1.VirtualBoxインストール

 ダウンロード→インストール

2.Vagrantインストール

 上と同様

3.CentOS7環境作成

CentOS7 を追加。
VirtualBoxのためproviderは 3 。

$ vagrant box add centos/7
==> box: Loading metadata for box 'centos/7'
    box: URL: https://atlas.hashicorp.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

Enter your choice: 3
==> box: Adding box 'centos/7' (v1708.01) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1708.01/providers/virtualbox.box
==> box: Successfully added box 'centos/7' (v1708.01) for 'virtualbox'!

完了後box確認。
下記のように表示されればOK。

$ vagrant box list
centos/7 (virtualbox, 1708.01)

仮想マシン環境用ディレクトリ作成

$ mkdir centos7

initで初期設定

$ cd centos7/
$ vagrant init centos/7
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$ ll
total 8
-rw-r--r--  1 sudachi808  staff  3352  9 21 20:24 Vagrantfile

upで仮想マシン起動

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' is up to date...
==> default: There was a problem while downloading the metadata for your box
==> default: to check for updates. This is not an error, since it is usually due
==> default: to temporary network problems. This is just a warning. The problem
(以下省略)

起動確認。
以下の表示が出ればOK。

$ vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

PostgreSQL11インストール

作成した仮想マシンに接続

$ vagrant ssh

リポジトリパッケージをインストール

# yum -y install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm
(省略)
インストール:
  pgdg-centos11.noarch 0:11-2

完了しました!

PostgreSQLをサーバとして利用するため、postgresql11-server

# yum -y install postgresql11-server
(省略)
インストール:
  postgresql11-server.x86_64 0:11.0-1PGDG.rhel7

依存性関連をインストールしました:
  libicu.x86_64 0:50.1.2-15.el7
  postgresql11.x86_64 0:11.0-1PGDG.rhel7
  postgresql11-libs.x86_64 0:11.0-1PGDG.rhel7

完了しました!

DB初期化

$ sudo /usr/pgsql-11/bin/postgresql11-setup initdb

設定完了。
PostgreSQLサービス起動。

systemctl start postgresql-11.service

サービス自動起動にする設定。

$ systemctl enable postgresql-11.service

ちなみにサービス終了

$ systemctl stop postgresql-11.service

ホストOSから仮想マシンのDBへpgAdminで接続

仮想マシン側の設定。confファイルの編集。

# vi /var/lib/pgsql/11/data/postgresql.conf
/var/lib/pgsql/11/data/postgresql.conf
# 59行目
# listen_addresses = 'localhost' 

# 63行目
# port = 5432                # (change requires restart)

上記を以下のように変更

/var/lib/pgsql/11/data/postgresql.conf
# 他のホストからのアクセスを受け取る
listen_addresses = '*'

port = 5432                # (change requires restart)

pg_hba.confファイルの編集。
全てのホストから認証なしで接続できるよう変更。検証環境なのでこれで問題なし。

# vi /var/lib/pgsql/11/data/pg_hba.conf
/var/lib/pgsql/11/data/pg_hba.conf
# 86行目
host    all             all        127.0.0.1/32                 trust

以下のように編集。

host    all             all             all                     trust

VirtualBoxの設定→ネットワーク→高度→ポートフォワーディングから、
プロトコル:TCP
ホストIP:127.0.0.1
ホストポート:5432
ゲストポート:5432
上記のように値を追加。

これでpgadminから接続が可能です。

以上で設定完了。

慣れると簡単ですが、疲れました。

5
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
9