server_encoding

A read-only parameter showing the current database's encoding

server_encoding is a read-only configuration parameter reporting the encoding in which the current database was created.

server_encoding was added in PostgreSQL 7.4.

Usage

server_encoding is a textual representation of the encoding column in pg_database. While this information could be useful when retrieving metadata about the database, the current value of client_encoding will usually be more relevant when interpreting string data.

Change history

Examples

Basic usage example for server_encoding:

postgres=# SHOW server_encoding;
 server_encoding 
-----------------
 UTF8
(1 row)

Example with a different encoding:

postgres=# CREATE DATABASE foo TEMPLATE 'template0' LOCALE 'ja_JP.EUC_JP' ENCODING 'EUC_JP';
CREATE DATABASE

postgres=# \c foo
You are now connected to database "foo" as user "postgres".

foo=# SHOW server_encoding;
 server_encoding 
-----------------
 EUC_JP
(1 row)

foo=# SHOW client_encoding;
 client_encoding 
-----------------
 UTF8
(1 row)

Use current_setting() to retrieve the value for further manipulation in a query:

postgres=# SELECT 'The server encoding is: ' || current_setting('server_encoding');
           ?column?           
------------------------------
 The server encoding is: UTF8
(1 row)

The database encoding can be also be queried directly from pg_database:

postgres=# SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = current_database();
 pg_encoding_to_char 
---------------------
 UTF8
(1 row)

Categories

GUC configuration item, Locale and character set handling, Preset configuration item

See also

client_encoding, pg_encoding_to_char()