简体   繁体   中英

PostgreSQL: Warning: Console code page (437) differs from Windows code page (1252)

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?

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 enter psql . Using chcp without parameters gives you the current codepage.

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

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

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

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%]: "

或者您可以简单地在命令提示符窗口中键入cmd.exe /c chcp 1252

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%]: "

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.

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.

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

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).

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.' ie "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.

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

  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM