51

Using PostgreSQL when I connect to a db using \c testdb inside PostgreSQL Database SQL Prompt. I successfully connect to the db but getting the following warning:

postgres-# \c testdb
WARNING: Console code page (437) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
You are now connected to database "testdb" as user "postgres".
testdb-#

What does this warning mean? How to resolve it?

Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57

18 Answers18

52

From the psql documentation:

psql is built as a "console application". Since the Windows console windows use a different encoding than the rest of the system, you must take special care when using 8-bit characters within psql. If psql detects a problematic console code page, it will warn you at startup.

To change the console code page, two things are necessary: Set the code page by entering cmd.exe /c chcp 1252. (1252 is a code page that is appropriate for German; replace it with your value.) If you are using Cygwin, you can put this command in /etc/profile.

So to remove that warning you need to execute chcp 1252 before you enterpsql. Using chcp without parameters gives you the current codepage.

dvdgsng
  • 1,691
  • 16
  • 27
  • I am facing the same issue. I tried executing the code 1252 but error still persist. In your solution, you mentioned 2 things to do but described only 1. Could you please explain the second thing to do – Abhishek Sep 24 '17 at 16:42
  • @Abhishek I'm not sure what you're asking. `chcp` shows the current encoding (aka code page). `chcp 1252` changes the encoding to `Latin 1 / Western European`, which is used for German. More details on Windows code pages see wikipedia https://en.wikipedia.org/wiki/Windows_code_page – dvdgsng Oct 02 '17 at 16:53
  • what's the 2nd thing? – Woodchuck Dec 03 '17 at 21:35
  • I assume this is the "Notes for Windows Users" referenced in the warning: https://www.postgresql.org/docs/current/static/app-psql.html (toward bottom of page) – Woodchuck Dec 03 '17 at 22:44
  • 3
    It's helpful to add `chcp 1252` to the psql bat file itself, which on my machine is located at "C:\Program Files\PostgreSQL\12\scripts\runpsql.bat". – Hutch Moore Jul 29 '20 at 18:19
  • 1
    This one helped me change it more widely on windows although someone reported issue with some program https://superuser.com/a/1435645/1064813 – Petri Ryhänen Mar 11 '22 at 14:41
  • @Abhishek, fwiw, dvdgsng provided an excerpt from the psql docs, but not all of it. It is missing the second instruction which is Set the console font to Lucida Console, because the raster font does not work with the ANSI code page. You can find it here: https:/postgresql.org/docs/14/app-psql.html. I am not sure though if this second step solves any problems you may encounter. The simplest way I could resolve the issue is by creating a simple batch script something like this: ```chcp 1252; psql -U postgres``` – Edvaaart Aug 14 '23 at 08:39
26

The default codepage for CMD.exe is different than the default for postgres... To change for CMD.exe using the REGISTRY try this:

  1. Start -> Run -> regedit
  2. Go to [HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
  3. Add new string value named "Autorun" with value "chcp 1252"

Then reopen CMD.exe

user3344137
  • 361
  • 3
  • 4
  • 4
    I know this is over 2 years old, but since it has no accepted answer, I would like to say that this worked for me. – Engels Leonhardt Mar 20 '20 at 13:54
  • It works even if one (me) uses psql command line tool installed with the postgres run directly (my Command Prompt doesn't recognise psql command because I don't put it in the path and I prefer it this way). It changes the code page of the prompt that I run from the tool. – Ola Sep 14 '22 at 05:33
  • The encoding needed to work with the Postgres depends on the database encoding though, and that needs to be set in a code page of a specific Command Prompt where the Postgres is running if it is different than the globally set code page for the Console (cmd.exe/Command Prompt) which can be set by this method. – Ola Sep 14 '22 at 06:07
13

To make it even more obvious, the file to which @user3423801 is adding the line

cmd.exe /c chcp 1252

is in the scripts directory where you installed Postgre.

For example, in my case it is

C:\Program Files\PostgreSQL\9.3\scripts\runpsql.bat
numbers longer
  • 331
  • 1
  • 3
  • 9
  • 2
    This isn't working for me. It works when I open the terminal through the batch file. But when I use `sql` the warning still pops up. – user2361174 Dec 27 '16 at 09:32
8

Open cmd.exe and run regedit.

Go to Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

New a string value named: Autorun and change the value to be chcp 1252

Done.

Reference: https://stackoverflow.com/a/30100565/8396969

6

Please don't assume that Unix fixes work for Windows Users. For Windows 10, and PostgreSQL 12, combining the answers by "user3423801" and "numbers longer" worked for me. (The Windows Registry hack would not work. I did not try rebooting yet.) It is better to fix it in the PSQL startup script anyway.

The file location C:\Program Files\PostgreSQL\12\scripts contains the file runpsql.bat, into which you must insert the cmd.exe /c chcp 1252 command in the right location. So the top of your edited file should look like the 5 or 6 lines below.

@echo off

REM Copyright (c) 2012-2014, EnterpriseDB Corporation.  All rights reserved

REM PostgreSQL server psql runner script for Windows

cmd.exe /c chcp 1252

SET server=localhost
SET /P server="Server [%server%]: "
Rich Lysakowski PhD
  • 2,702
  • 31
  • 44
4

The answer of dvdgsng is correct but with code example is more obviously.

@echo off

REM Copyright (c) 2012-2014, EnterpriseDB Corporation.  All rights reserved

REM PostgreSQL server psql runner script for Windows

cmd.exe /c chcp 1252

SET server=localhost
SET /P server="Server [%server%]: "
Skwal
  • 2,160
  • 2
  • 20
  • 30
4

Or you can simply type cmd.exe /c chcp 1252 in the Command Prompt window.

iusting
  • 7,850
  • 2
  • 22
  • 30
  • If what the OP needs is the 1252 code page. The encoding needed depends on the database encoding. The code page number of more universal utf-8 encoding 65001. – Ola Sep 14 '22 at 05:53
3

you just go to the power-shell or cmd.exe and type the command chcp 1252 or whatever the page number it wants "the one that is the windows code page". If the problem still persists, just open the console properties 'By clicking the power shell icon on the top left of the console window and choosing properties from the drop-down menu' and change the font to "Lucida Console". It worked for me, But you have to open power-shell as an Administrator.

Ahmed Appas
  • 103
  • 1
  • 5
2

The answers above are okay, but don't mention anywhere that Windows 1252 encoding is good for English language versions of Windows AND the other Western European Languages. This completes the answer for those people who may get confused about the aforementioned application to German language encoding. Yes it works for English without umlauts and other special characters needed for German, Spanish, French, Italian, Romanian, Hungarian, etc.

https://en.m.wikipedia.org/wiki/Windows-1252

What is Windows 1252 encoding?

Windows-1252 or CP-1252 (code page 1252) is a single-byte character encoding of the Latin alphabet, used by default in the legacy components of Microsoft Windows for English and some other Western languages (other languages use different default encodings).

Rich Lysakowski PhD
  • 2,702
  • 31
  • 44
1

After much digging for an answer that made sense to me, I found this help email chain at the PostgreSQL site which basically says to run chcp 1252 from inside an open command window. I was then able to run my PostgreSQL commands without the code warning.

NOTE: this change does not persist so you have to run it every time you open a new command window where you plan to use PostgreSQL commands.

amraby
  • 1,228
  • 10
  • 11
1

For Postgres 11

"WARNING: Console code page (437) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details."

If you aren't an administrator on your machine Add a line "chcp 1252" to the pg_env.bat script found in the base directory of your postgres installation.' i.e. "C:\Program Files\PostgreSQL\11"

If you are and Administrator on your machine you can modify the registry to run the line everytime you run "cmd.exe" as mentioned above.

1

I couldn't figure out how to set it for Cygwin globally. This seemed to work though in my bash script

#!/bin/bash
cmd.exe /c chcp 1252 && psql -h myserver.postgres.database.azure.com -U myuser@prod-au -d mydatabase
Damien Sawyer
  • 5,323
  • 3
  • 44
  • 56
1

In the command prompt terminal just type in chcp 1252; no need to open any file or go to any file path

Example:

C:\Users\dsmith> chcp 1252   

After that, when you log in to psql from this cmd terminal, you will not see the problem of console code warning

helvete
  • 2,455
  • 13
  • 33
  • 37
0

Basically, set your console application encoding from 8-bit to utf-8 Windows 1252.

For git bash users run the command chcp.com 1252 before running postgres

chcp is a windows console command, so to execute it on git bash you might need to add .com

git bash can't extend chcp to a full executable on its own, so you need to type the full command.

here

go je jo
  • 311
  • 4
  • 8
0
  1. On the terminal screen go to the following directory;
    C:\Program Files\PostgreSQL\14\bin

note: whichever database version you are using, go to the bin folder of the db version file.

  1. Before creating a user or accessing the database user, you must write the following code;
    cmd.exe /c chcp 1252
0

On windows, edit C:\Program Files\PostgreSQL\xx\scripts\runpsql.bat

If you do not have write access, you may edit the file via an administrator command prompt. You may also go into the file's properties and edit the Security Permissions to include write access for your user/group.

Add the following on its own line before the SET variables: chcp <console_code>

Replace <console_code> with the code given to you by the psql shell warning.

Finally right click the very top of your console, go into the properties, and change the font to Lucida Console. This is a font which will work correctly with the code page.

More information can be found here: https://www.postgresql.org/docs/15/app-psql.html

pyknight202
  • 1,415
  • 1
  • 5
  • 22
0

Im currently using psql 15.2 and I use it with the git bash terminal.

To be able to use it from anywhere I first added C:\Program Files\PostgreSQL\15\bin to my system environment variables. Then I noticed when executing the command psql, I recieved the same error as shown in this thread.

The solution I came up with is:

I added the following lines to my .bash_profile file (C:\Users<user>.bash_profile)

# psql conf

chcp.com 1252 >/dev/null
alias psql='psql.exe'

# end of psql conf

Notice that when running the command chcp from the git bash terminal doesn't work, it works when you run chcp.com.

And after running chcp.com 1252, when you run psql -U <username> and logging to an account, the error shows up again. Just when you run psql.exe -U <username> the error doesn't show anymore. That's why I added the alias.

If anyone knows why in this case running psql is different than running psql.exe, please let me know.

Svic321
  • 23
  • 5
0

For Windows users:

I couldn't change the C:\Program Files\PostgreSQL\xx\scripts\runpsql.bat file because I don't have admin rights (work computer). So instead I opened the psql start up file (%appdata%\postgresql\psqlrc.conf, if it's not there you can just create one) and simply added \! chcp 1252. Restarted the server and works like a charm.

mikibok
  • 35
  • 4