Skip to content

Instantly share code, notes, and snippets.

@Demacr
Created February 16, 2020 14:57
Show Gist options
  • Save Demacr/3e3bcd4885d2f95bfe108b30bf90654c to your computer and use it in GitHub Desktop.
Save Demacr/3e3bcd4885d2f95bfe108b30bf90654c to your computer and use it in GitHub Desktop.
Postgresql accept 0 or 1 as boolean

Digging in to the dark magic of postgresql, i discovered that the integer to boolean cast can be changed from an exlicit to an automatic coersion. Then changed back when done...

update pg_cast set castcontext='a' where casttarget = 'boolean'::regtype;

Then load with pg_dump.

When done, set back the casting to its default with...

update pg_cast set castcontext='e' where casttarget = 'boolean'::regtype;

Important note: don't do this unless you know what it involves. The catalogs aren't guaranteed to be compatible version to version, and something that's safe(ish) to do now could be quite bad to do in a future version, or not work at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment