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 6928565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:12:17+00:00 2026-05-27T11:12:17+00:00

I am trying to convert a MySQL database to Postgres. It is frustrating, but

  • 0

I am trying to convert a MySQL database to Postgres. It is frustrating, but proceeding along steadily. One issue that has me stumped is to convert the MySQL SET data type to Postgres. A SET data type in MySQL is not the same as the plain ENUM type, and can’t be emulated with a CHECK constraint.

As far as I understand, a SET type allows storing zero or more values from the set in the column. So, something like the following in MySQL

CREATE TABLE foo (color SET('red','green','blue'));

will allow any of the following as valid values

''
'red'
'red,blue'
'green,red'

and so on. A close approximation in Postgres is

CREATE TABLE foo (
    color VARCHAR(10) NOT NULL, 
    CHECK (color IN ('red','green','blue'))
);

but the above doesn’t allow ‘red,blue’ or ‘green,red’ and so on.

Of course, the above is just a simplification. The actual database is fairly more complicated with about half a dozen columns defined as SET.

Suggestions?

  • 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-27T11:12:18+00:00Added an answer on May 27, 2026 at 11:12 am

    You could use an array for the column and an “is contained by” operator for the CHECK constraint:

    create table pancakes (
        color varchar(10)[] not null,
        check (color <@ ARRAY['red', 'green', 'blue']::varchar[])
    );
    

    And then things like this happen:

    => insert into pancakes values (ARRAY['red']);
    INSERT 0 1
    => insert into pancakes values (ARRAY['red','green','blue']);
    INSERT 0 1
    => insert into pancakes values (ARRAY['red','green','blue','black']);
    ERROR:  new row for relation "pancakes" violates check constraint "pancakes_color_check"
    => select * from pancakes;
          color       
    ------------------
     {red}
     {red,green,blue}
    (2 rows)
    

    This will allow {red,red} in the column though; if disallowing {red,red} is important, then you could add a function to check for unique color values in the array and adjust the CHECK constraint:

    create function has_unique_colors(varchar[]) returns boolean as $$
        select (select count(distinct c) from unnest($1) as dt(c)) = array_length($1, 1);
    $$ language sql;
    
    create table pancakes (
        color varchar(10)[] not null,
        check (color <@ ARRAY['red', 'green', 'blue']::varchar[] and has_unique_colors(color))
    );
    

    Another option would be a pile of association tables with simple scalar values in the columns. However, this might be cumbersome if you have six of these columns. You could also use Erwin’s version of the function if you needed to worry about NULLs in the “sets”:

    create function has_unique_colors(varchar[]) returns boolean as $$
        select not exists(select c from unnest($1) dt(c) group by 1 having count(*) > 1);
    $$ language sql;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to convert a mysql database to a sqlite db file but
I'm trying to convert some code that worked great in VB, but I can't
I'm working on a PHP-based webapp that has an existing MySQL database where all
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
I am trying to extract unix timestamp from mysql and convert it to ISO8601
Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm trying to convert an XML document into a dataset that I can import
I am basically trying to convert a string similar to this one: 2011-11-9 18:24:12.3
I am trying to convert this MSSQL QUERY to MYSQL Query looks like this
I'm trying to convert a greek database to utf8. At this point, I've figured

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.