Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4066512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:08:18+00:00 2026-05-20T16:08:18+00:00

(Not a duplicate of 4079956 ) I have an SQL_ASCII database, LC_CTYPE = LC_COLLATION

  • 0

(Not a duplicate of 4079956)

I have an SQL_ASCII database, LC_CTYPE=LC_COLLATION="C", which contains mostly ASCII data as well as some non-ASCII characters from some codepage, say LATIN1.

I want to transcode, in-place (no pg_dump/pg-restore), all non-ASCII codepoints from the LATIN1 codepage to UTF-8 then alter the database encoding to UTF-8, e.g.:

-- change encoding first, transcode data after
UPDATE pg_database SET encoding=pg_char_to_encoding('UTF8')
  WHERE datname='sqlasciidb';
UPDATE tbl SET str=convert_from(str::bytea, 'LATIN1')
  WHERE str::bytea<>convert_from(str::bytea, 'LATIN1')::bytea;

or

-- transcode data first, change encoding after
CREATE DOMAIN my_varlena AS bytea;
CREATE CAST (my_varlena AS text) WITHOUT FUNCTION;
UPDATE tbl SET str=convert(str::bytea, 'LATIN1','UTF8')::my_varlena::text
  WHERE str::bytea<>convert(str::bytea, 'LATIN1', 'UTF8');
DROP DOMAIN my_varlena CASCADE;
UPDATE pg_database SET encoding=pg_char_to_encoding('UTF8')
  WHERE datname='sqlasciidb';

What, if anything, is wrong with the above approach?

Some problems I can see:

  • after pg_database is updated, all connections to the database should be closed and reopened for the backend to take into account the new encoding
  • all indexes based on the altered columns should be rebuilt

Anything else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:08:19+00:00Added an answer on May 20, 2026 at 4:08 pm

    Looks like you’ve got the main gist of it. I assume you’ve already tried this with a test database? I did give it a quick test when suggesting it to someone and it seemed to work ok for me, although this was far from a thorough test.

    My gut feel is to transcode first and change encoding after, because while the database is still in SQL_ASCII, you aren’t going to have to deal with errors from postgresql trying to interpret not-yet-transcoded or improperly-transcoded data, and can look at data with relative impunity. OTOH changing the encoding first guarantees that only subsequently-connecting backends will write data in UTF8…

    Also have a check for things like function bodies, view definitions, constraint definitions etc. that may need transcoding too? (you’d hope not, but…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a process updating the database which uses the following SQL IF NOT
I have already read Entity Framework One-To-One Mapping Issues and this is not duplicate
In order to do not duplicate code, I've decided some time ago, to refactor
I saw this question and some similar and I think it's not duplicate :
I Have two Queue and i Want To Delete Data is Duplicate and i
Possible Duplicate: Not possible to launch a file on a network using Java Desktop?
PreNote: This is not a duplicate. I've checked several questions and mine is different
This is NOT a duplicate question, and the problem is driving me crazy. I
I hope this question is not a duplicate but I didn't find anything equivalent
I see that Pandas does not allow duplicate time series indexes yet ( https://github.com/pydata/pandas/issues/643

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.