
こんな悩みを解決できる記事を書きました!
僕は現役フリーランスエンジニア(歴9年)で、資格は13個保有しています。
結論、VagrantでPostgreSQL環境を構築するのは非常に簡単です。


本記事を読めば、VagrantでPostgreSQL環境を構築する方法が理解できるので早速見ていきましょう!



すぐ読み終わるので、ぜひ最後まで読んでくださいね♪
目次
VagrantでPostgreSQL環境を構築する手順
VagrantでPostgreSQL環境を構築する手順は以下の通りです。
- VagrantでCentOS環境を構築。
- Postgreをインストール。
- データベースを作成。
VagrantでPostgreSQL環境を構築する方法
では早速、環境構築をしていきましょう。


任意の場所にフォルダを作成します。
$ vagrant init centos/7
先ほど作成したフォルダにVagrantファイルを作成します。
$ vagrant up
Vagrantを起動します。
$ vagrant ssh
SSHでログインします。
[vagrant@localhost ~]$ sudo yum install postgresql-server
Postgreをインストールします。
[vagrant@localhost ~]$ sudo service postgresql initdb
データベースを作成します。
[vagrant@localhost ~]$ sudo service postgresql start
Postgreを起動します。
[vagrant@localhost ~]$ sudo chkconfig postgresql on
Postgreの自動起動をONにします。
[vagrant@localhost ~]$ sudo -u postgres psql
postgresユーザでPostgreにログインします。
[vagrant@localhost ~]$ sudo -u postgres createdb sampledb
DBを作成します。
[vagrant@localhost ~]$ sudo -u postgres psql sampledb
先ほど作成したsampledbにログインします。
sampledb=# create user vagrant;
ユーザを作成します。
[vagrant@localhost ~]$ psql sampledb
CentOSにログインしているユーザもvagrantなので、postgreへのログイン時にユーザ名を省略できます。
sampledb=# select usename from pg_user;
usename
----------
postgres
vagrant
(2 rows)
ユーザを確認し、問題なければ環境構築は完了です。
まとめ
今回は、VagrantでPostgreSQL環境を構築する方法について解説しました。



最後までお読みいただき、ありがとうございました!
- クソおすすめ本



海外のエンジニアがどういった思考で働いているかが理解できます。
海外に行く気はないけど海外エンジニアの動向が気になる雑魚エンジニアにおすすめです(本当におすすめな本しか紹介しないのでご安心を)。