Cannot install Synapse

root@thibaut:~# apt install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
postgresql is already the newest version (9.6+181+deb9u2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
6 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Setting up libwbclient0:armhf (2:4.5.12+dfsg-2+deb9u4) ...
Setting up samba-libs:armhf (2:4.5.12+dfsg-2+deb9u4) ...
Setting up raspberrypi-sys-mods (20181127) ...
Installing new version of config file /etc/profile.d/at-dbus-fix.sh ...
Setting up samba-common (2:4.5.12+dfsg-2+deb9u4) ...
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
Setting up libsmbclient:armhf (2:4.5.12+dfsg-2+deb9u4) ...
Setting up smbclient (2:4.5.12+dfsg-2+deb9u4) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Processing triggers for man-db (2.7.6.1-2) ...
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").

root@thibaut:~# sudo -u postgres psql
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
/bin/bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
could not change directory to "/root": Permission denied
psql: could not connect to server: No such file or directory
	Is the server running locally and accepting
	connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

Could the problem be linked to the language configuration?

postgresql is already the newest version (9.6+181+deb9u2).

It look like you didn’t really purge postgresql. If you don’t firstly purge postgresql it won’t be possible to install it cleanly.

Same problem.
According to the log, the reason comes from the fact I didn’t uninstall postgresql properly.

Here is what I did:

apt-get --purge remove postgresql\*
rm -r /etc/postgresql/
rm -r /etc/postgresql-common/
rm -r /var/lib/postgresql/
userdel -r postgres
groupdel postgres

For the 2 last lines, the users were not found and I don’t know how to check what else it could be.
Also, I still have this message asking to check my language settings.
Here is the detail of these commands:

root@thibaut:~# apt-get --purge remove postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  postgresql*
0 upgraded, 0 newly installed, 1 to remove and 11 not upgraded.
After this operation, 61.4 kB disk space will be freed.
Do you want to continue? [Y/n] y
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
(Reading database ... 59202 files and directories currently installed.)
Removing postgresql (9.6+181+deb9u2) ...
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
root@thibaut:~# rm -r /etc/postgresql/
root@thibaut:~# rm -r /etc/postgresql-common/
root@thibaut:~# rm -r /var/lib/postgresql/
root@thibaut:~# userdel -r postgres
userdel: postgres mail spool (/var/mail/postgres) not found
userdel: postgres home directory (/var/lib/postgresql) not found
root@thibaut:~# userdel -r postgresql
userdel: user 'postgresql' does not exist
root@thibaut:~# groupdel postgres
groupdel: group 'postgres' does not exist
root@thibaut:~# apt-get install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  postgresql-doc
The following NEW packages will be installed:
  postgresql
0 upgraded, 1 newly installed, 0 to remove and 11 not upgraded.
Need to get 0 B/55.8 kB of archives.
After this operation, 61.4 kB of additional disk space will be used.
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LC_COLLATE = "C",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
apt-listchanges: Can't set locale; make sure $LC_* and $LANG are correct!
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "en_GB.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_GB.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package postgresql.
(Reading database ... 59200 files and directories currently installed.)
Preparing to unpack .../postgresql_9.6+181+deb9u2_all.deb ...
Unpacking postgresql (9.6+181+deb9u2) ...
Setting up postgresql (9.6+181+deb9u2) ...
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = "en_US.UTF-8",
	LANG = "C"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

Ok now,

Could you try sudo -u postgres psql ?

root@thibaut:~# sudo -u postgres psql
sudo: unknown user: postgres
sudo: unable to initialize policy plugin

Hi Thibaut,
did you solve your postgres problem finally?

Hi Rocky,
Still not.
Despite re-installing the OS from scratch, I keep having the same problem.

It looks like I solved the language error by making that “locale” gets the same language for all the lines.

Now, the current error log is the following one:

2019-03-13 20:09:29,920: DEBUG - + test -n synapse__2
2019-03-13 20:09:29,921: DEBUG - + ynh_replace_string __NAME__ synapse__2 /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:29,922: DEBUG - + local delimit=@
2019-03-13 20:09:29,923: DEBUG - + local match_string=__NAME__
2019-03-13 20:09:29,924: DEBUG - + local replace_string=synapse__2
2019-03-13 20:09:29,925: DEBUG - + local workfile=/etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:29,926: DEBUG - + match_string=__NAME__
2019-03-13 20:09:29,927: DEBUG - + replace_string=synapse__2
2019-03-13 20:09:29,928: DEBUG - + sudo sed --in-place s@__NAME__@synapse__2@g /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,029: DEBUG - + test -n /opt/yunohost/matrix-synapse__2
2019-03-13 20:09:30,031: DEBUG - + ynh_replace_string __FINALPATH__ /opt/yunohost/matrix-synapse__2 /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,032: DEBUG - + local delimit=@
2019-03-13 20:09:30,033: DEBUG - + local match_string=__FINALPATH__
2019-03-13 20:09:30,034: DEBUG - + local replace_string=/opt/yunohost/matrix-synapse__2
2019-03-13 20:09:30,035: DEBUG - + local workfile=/etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,036: DEBUG - + match_string=__FINALPATH__
2019-03-13 20:09:30,037: DEBUG - + replace_string=/opt/yunohost/matrix-synapse__2
2019-03-13 20:09:30,038: DEBUG - + sudo sed --in-place s@__FINALPATH__@/opt/yunohost/matrix-synapse__2@g /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,139: DEBUG - + '[' /_matrix '!=' / ']'
2019-03-13 20:09:30,140: DEBUG - + ynh_replace_string '^#sub_path_only' '' /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,141: DEBUG - + local delimit=@
2019-03-13 20:09:30,141: DEBUG - + local 'match_string=^#sub_path_only'
2019-03-13 20:09:30,142: DEBUG - + local replace_string=
2019-03-13 20:09:30,143: DEBUG - + local workfile=/etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,143: DEBUG - + match_string='^#sub_path_only'
2019-03-13 20:09:30,144: DEBUG - + replace_string=
2019-03-13 20:09:30,144: DEBUG - + sudo sed --in-place 's@^#sub_path_only@@g' /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,145: DEBUG - + ynh_store_file_checksum /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,146: DEBUG - + local checksum_setting_name=checksum__etc_nginx_conf.d_thibaut.fr.to.d_synapse__2.conf
2019-03-13 20:09:30,146: DEBUG - ++ sudo md5sum /etc/nginx/conf.d/thibaut.fr.to.d/synapse__2.conf
2019-03-13 20:09:30,147: DEBUG - ++ cut '-d ' -f1
2019-03-13 20:09:30,248: DEBUG - + ynh_app_setting_set synapse__2 checksum__etc_nginx_conf.d_thibaut.fr.to.d_synapse__2.conf fa425cc6e51196d7fadf604064330f47
2019-03-13 20:09:30,250: DEBUG - + sudo yunohost app setting synapse__2 checksum__etc_nginx_conf.d_thibaut.fr.to.d_synapse__2.conf --value=fa425cc6e51196d7fadf604064330f47 --quiet
2019-03-13 20:09:31,656: DEBUG - + sudo systemctl reload nginx
2019-03-13 20:09:32,259: DEBUG - + ynh_exit_properly
2019-03-13 20:09:32,259: WARNING - Job for nginx.service failed because the control process exited with error code.
2019-03-13 20:09:32,261: DEBUG - + local exit_code=1
2019-03-13 20:09:32,262: WARNING - See "systemctl status nginx.service" and "journalctl -xe" for details.
2019-03-13 20:09:32,263: DEBUG - + '[' 1 -eq 0 ']'
2019-03-13 20:09:32,264: WARNING - !!
2019-03-13 20:09:32,266: DEBUG - + trap '' EXIT
2019-03-13 20:09:32,266: WARNING -   synapse__2's script has encountered an error. Its execution was cancelled.
2019-03-13 20:09:32,268: DEBUG - + set +eu
2019-03-13 20:09:32,268: WARNING - !!
2019-03-13 20:09:32,270: DEBUG - + echo -e '!!\n  synapse__2'\''s script has encountered an error. Its execution was cancelled.\n!!'
2019-03-13 20:09:32,271: WARNING - 
2019-03-13 20:09:32,272: DEBUG - + type -t ynh_clean_setup
2019-03-13 20:09:32,273: DEBUG - + ynh_die
2019-03-13 20:09:32,274: DEBUG - + echo ''
2019-03-13 20:09:32,274: DEBUG - + exit 1