Configure Symfony 4 with PostgreSQL on Ubuntu or Mac

How to configure Symfony 4 with PostgreSQL on Ubuntu or Mac OS

Install PostgreSQL on Ubuntu  
Easily install PostgreSQL from the command line with apt-get.

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

Install PostgreSQL on Mac   
On Mac OS, go to your terminal and use brew as shown here.

brew install postgresql
postgres -V to see the version

Use composer to install Doctrine   
If Doctrine is not already installed, then from the command line in your Symfony 4 project directory, use composer to require doctrine.

composer require doctrine/doctrine-bundle

Configure config/doctrine.yml
Change the dbal values as shown below.

...
doctrine:
dbal:
    driver: 'pdo_pgsql'
    charset: utf8
...

Edit the .env file.   
PostgreSQL may create a user matching the user you logged in as. You will need to change the db_user and password to whatever is appropriate. Replace the mysql settings with the PostgreSQL settings as shown below:

...

DATABASE_URL=pgsql://db_user:db_password@127.0.0.1:5432/db_name

...

Now you can use the command line to create the database!

php bin/console doctrine:database:create

Leave a Reply

Your email address will not be published. Required fields are marked *