upleveled / system-setup

Set up a PERN development environment on Windows, macOS and Linux
28 stars 19 forks source link

Add localization configuration to Postgres installation #80

Closed Eprince-hub closed 1 month ago

Eprince-hub commented 1 month ago

Closes https://github.com/upleveled/system-setup/issues/73

Add the Perl commands to set the lc_time and lc_messages to English during Postgres installations. Remove the LC_ALL setting from the macOS setup, as it is no longer required. Setup a PGDATA variable for the Linux system as this is not set by the current commands

Windows

The current setup doesn't set language

echo "export PATH=\$PATH:\"/c/Program Files/PostgreSQL/16/bin\"" >> "$USERPROFILE/.bash_profile"
echo "export PGDATA=\"/c/Program Files/PostgreSQL/16/data\"" >> "$USERPROFILE/.bash_profile"
echo "export PSQL_PAGER=\"less --chop-long-lines --header 1\"" >> "$USERPROFILE/.bash_profile"
source "$USERPROFILE/.bash_profile"

1111newnow

After the PR, the language is set

echo "export PATH=\$PATH:\"/c/Program Files/PostgreSQL/16/bin\"" >> "$USERPROFILE/.bash_profile"
echo "export PGDATA=\"/c/Program Files/PostgreSQL/16/data\"" >> "$USERPROFILE/.bash_profile"
echo "export LC_ALL=en_US.UTF-8" >> "$USERPROFILE/.bash_profile"
echo "export PSQL_PAGER=\"less --chop-long-lines --header 1\"" >> "$USERPROFILE/.bash_profile"
source "$USERPROFILE/.bash_profile"

11111newnew

postgresql.conf file spec

One parameter is specified per line. The equal sign between name and value is optional. Whitespace is insignificant (except within a quoted parameter value) and blank lines are ignored. Hash marks (#) designate the remainder of the line as a comment. Parameter values that are not simple identifiers or numbers must be single-quoted. To embed a single quote in a parameter value, write either two quotes (preferred) or backslash-quote. If the file contains multiple entries for the same parameter, all but the last one are ignored.

https://www.postgresql.org/docs/current/config-setting.html#:~:text=Whitespace%20is%20insignificant%20(except%20within%20a%20quoted%20parameter%20value)%20and%20blank%20lines%20are%20ignored

Perl error when file not found

 ~ perl -i -pe 's/^[#\s]*lc_messages\s*=.+$/lc_messages = '\''en_US.UTF-8'\''/' testings/testee.conf
Can't open testings/testee.conf: No such file or directory.

Setting the lc_messages in the postgresql.conf file

macOS

To edit the postgres.conf file and set the `lc_message = 'en_US.UTF-8'. Any of the following commands can be used

After command

Screenshot 2024-07-18 at 18 27 22

Other possible commands on macOS Using perl

perl -i -pe 's/^\s*lc_messages\s*=\s*.*/lc_messages = '\''en_US.UTF-8'\''/' "$PGDATA/postgresql.conf"

perl error: Can't open nothing/postgresql.conf: No such file or directory.

Using awk

awk '/^[[:space:]]*lc_messages[[:space:]]*=/{gsub(/=.*/, "= '\''en_US.UTF-8'\''")}1' "$PGDATA/postgresql.conf" > temp && mv temp "$PGDATA/postgresql.conf"

awk error:

 ~  awk '/^[[:space:]]*lc_messages[[:space:]]*=/{gsub(/=.*/, "= '\''en_US.UTF-8'\''")}1' "nothing/postgresql.conf" > temp && mv temp "nothing/postgresql.conf"
awk: can't open file nothing/postgresql.conf
 source line number 1

Linux

The PGDATA cannot be set in Linux as the location could change depending on the Linux distribution being run. The path to the postgresql.conf file should be determined dynamically

Using sed

sed -i -E "/^[[:space:]]*lc_messages[[:space:]]*=/ s/^#*.*$/lc_messages = 'eu_US.UTF-8'/" "$(sudo -u postgres psql --tuples-only --pset format=unaligned --command 'SHOW config_file;')"

OR

sed --in-place --regexp-extended "/^[[:space:]]*lc_messages[[:space:]]*=/ s/^#*.*$/lc_messages = 'eu_US.UTF-8'/" "$(sudo -u postgres psql --tuples-only --pset format=unaligned --command 'SHOW config_file;')"

Using perl

perl -i -pe 's/^\s*lc_messages\s*=\s*.*/lc_messages = '\''en_US.UTF-8'\''/' "$(sudo -u postgres psql --tuples-only --pset format=unaligned --command 'SHOW config_file;')"

Using awk

awk '/^[[:space:]]*lc_messages[[:space:]]*=/{gsub(/=.*/, "= '\''en_US.UTF-8'\''")}1' "$(sudo -u postgres psql --tuples-only --pset format=unaligned --command 'SHOW config_file;')" > temp && mv temp "$(sudo -u postgres psql --tuples-only --pset format=unaligned --command 'SHOW config_file;')"

Tested with a Linux + PostgreSQL Docker image setup using the progress installation from the system setup

Before command

Screenshot 2024-07-18 at 15 53 14

After command

Screenshot 2024-07-18 at 15 55 12

postgresql.conf file path

Screenshot 2024-07-18 at 15 51 58

Windows

Using hyper as this will enable us use the same commands we already used for macOS and Linux

Using sed

sed -i -E "/^[[:space:]]*lc_messages[[:space:]]*=/ s/^#*.*$/lc_messages = 'en_US.UTF-8'/" "$PGDATA/postgresql.conf"

Using perl

perl -i -pe 's/^\s*lc_messages\s*=\s*.*/lc_messages = '\''en_US.UTF-8'\''/' "$PGDATA/postgresql.conf"

Using awk

awk '/^[[:space:]]*lc_messages[[:space:]]*=/{gsub(/=.*/, "= '\''en_US.UTF-8'\''")}1' "$PGDATA/postgresql.conf" > temp && mv temp "$PGDATA/postgresql.conf"

Before command Screenshot 2024-07-19 104129

After command Screenshot 2024-07-19 104314

Errors when the file is not found Screenshot 2024-07-19 105331