2

I am trying to set client encoding for a session. I am running the following command on linux terminal (postgresql installed on a remote server.

psql -h localhost -p 5432 -U user -d dbase -c "SET client_encoding to 'LATIN1';"

Output: SET

psql -h localhost -p 5432 -U user -d dbase -c "show client_encoding"

Output: UTF8

Why is this happening? Do I need to run this command as a superuser? (I don't think so)

If I run the following commands on Pgadmin4 then it correctly show LATIN1 as the output.

SET client_encoding to 'LATIN1'

show client_encoding

The server encoding is set to UTF8.

4
  • 3
    SET client_encoding to 'LATIN1'; is not persistent, it is only set for the current session/connection. Commented Jan 3, 2018 at 22:04
  • yeah I want it for just one session. if you run the above 2 commands simultaneously then should it not output 'LATIN1' atleast for that session? Commented Jan 3, 2018 at 22:06
  • I understood my mistake writing psql command each time creates a new session hence the encoding defaults to original. How do I change it permanently? Commented Jan 3, 2018 at 22:09
  • postgresql.org/docs/current/static/config-setting.html Commented Jan 3, 2018 at 22:17

1 Answer 1

1

Setting the client encoding is only for the current session unless you set the environment variable PGCLIENTENCODING.

From postgresql documentation: If the environment variable PGCLIENTENCODING is defined in the client's environment, that client encoding is automatically selected when a connection to the server is made.

So, if you want the client encoding persist try setting this environment var.

Or if you want to execute multiple queries in just one session have a look at the -f parameter to set a file to parse. Ex: psql -d myDataBase -f myFile

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.