2. Homesteadを用いたsymfony4環境構築手順(DB設定)
概要
Homesteadを使用したsymfony4の環境構築(DB設定)についてやったことを纏めた。 既にHomesteadにインストールしてあるPostgreSQLにsymfony_dbというデータベースを作成する。下記のサイトを参考に環境構築を行った。
Using PostgreSQL with Symfony 4
環境構築
1. 必要なライブラリのインストール
$ composer require doctrine/doctrine-bundle
2. .envへのDB設定記述
# .env # customize this line! - DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name + DATABASE_URL=pgsql://homestead:secret@127.0.0.1:5432/symfony_db
ここに記述した情報をもとにDBが作成される。
3. config/doctrine.yamlの修正
doctrine: dbal: - driver: 'pdo_mysql' + driver: 'pdo_pgsql' charset: utf8
3. データベース作成
$ php bin/console doctrine:database:create Created database "symfony_db" for connection named default
4. 作成したデータベースの確認
データベースに接続し、テーブルが作成されているかを確認する。 Homesteadの場合、DBの初期パスワードはsecretとなっている。
vagrant@homestead:~/code$ psql -U homestead -h localhost Password for user homestead:(secret) ... homestead=# \l Name | Owner | Encoding | Collate | Ctype | Access privileges ------------+-----------+----------+-------------+-------------+----------------------- homestead | homestead | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | symfony_db | homestead | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (5 rows)