11

The Problem

I have a fresh setup of postgres 10.5 and symfony 4 application running on php 7.1. But when I try running migration. I keep getting the following Invalid value for parameter "client_encoding": "utf8mb4" error. enter image description here

Steps to reproduce

  1. On .env file by modify DSN to correct value based on the your settings for eg. mine wasDATABASE_URL="pgsql://postgres:password@db:5432/a_db".
  2. Create an entity (Any would do) using php bin/console make:entity
  3. Make Migration file php bin/console make:migration

Expected Result

I should have received Success message.

So my Question is

What did I miss here as I have followed the documentation?

Bikal Basnet
  • 1,639
  • 1
  • 21
  • 31

2 Answers2

30

So my actual client config in the postgres is utf8 not utf8mb4. It seems symfony does not automatically detects the version and database for us.

Symfony 4 has left the standard utf8mb4 for MYSQL in the config file config/packages/doctrine.yaml. This configuration file should not be forgotten to change based on these allowed configuration. So the problem was fixed when I changed the value

from

dbal: driver: 'pdo_mysql' server_version: '5.7' charset: utf8mb4 default_table_options: charset: utf8mb4 collate: utf8mb4_unicode_ci

to

dbal: driver: 'pdo_postgresql' server_version: '10.5' charset: utf8 default_table_options: charset: utf8 collate: utf8_unicode_ci

Bikal Basnet
  • 1,639
  • 1
  • 21
  • 31
  • Who ever posted negative voting.,It would be helpful if you post your comment. Why did my answer deserves vote instead of silently running away. – Bikal Basnet Sep 24 '18 at 04:58
  • 2
    Can't see any reason for the downvotes, so I'm giving you upvotes ;-) BTW: You can append the charset to `DATABASE_URL` inside `.env` like this: `DATABASE_URL="pgsql://postgres:password@db:5432/a_db?charset=UTF-8` Looks like this isn't really documented anywhere, so I opened this https://github.com/symfony/symfony-docs/pull/10410 – Thomas Landauer Sep 27 '18 at 22:44
  • I believe the `default_table_options` would be best to be removed so that the database defaults control those settings, instead of Doctrine. However `utf8_unicode_ci` is exclusive to MySQL and not available for PostgreSQL which uses `en_US.UTF-8` as in the [Doctrine DBAL test](https://github.com/doctrine/dbal/blob/2.7/tests/Doctrine/Tests/DBAL/Platforms/PostgreSQL91PlatformTest.php) – Will B. May 15 '19 at 05:50
4

For me it helped to add the encoding to the Database URL, like this:

postgres://api-platform:!ChangeMe!@db/api?charset=UTF-8

Though this problem only occurred in APP_ENV=prod.

Kim
  • 1,757
  • 1
  • 17
  • 32