How to alter Postgres table data based on its contents?

Posted by williamjones on Stack Overflow See other posts from Stack Overflow or by williamjones
Published on 2010-06-16T22:18:04Z Indexed on 2010/06/16 22:22 UTC
Read the original article Hit count: 208

Filed under:

This is probably a super simple question, but I'm struggling to come up with the right keywords to find it on Google.

I have a Postgres table that has among its contents a column of type text named content_type. That stores what type of entry is stored in that row.

There are only about 5 different types, and I decided I want to change one of them to display as something else in my application (I had been directly displaying these).

It struck me that it's funny that my view is being dictated by my database model, and I decided I would convert the types being stored in my database as strings into integers, and enumerate the possible types in my application with constants that convert them into their display names. That way, if I ever got the urge to change any category names again, I could just change it with one alteration of a constant. I also have the hunch that storing integers might be somewhat more efficient than storing text in the database.

First, a quick threshold question of, is this a good idea? Any feedback or anything I missed?

Second, and my main question, what's the Postgres command I could enter to make an alteration like this? I'm thinking I could start by renaming the old content_type column to old_content_type and then creating a new integer column content_type. However, what command would look at a row's old_content_type and fill in the new content_type column based off of that?

© Stack Overflow or respective owner

Related posts about postgresql