Skip to content

Instantly share code, notes, and snippets.

@mcnemesis
Created May 21, 2013 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mcnemesis/5621370 to your computer and use it in GitHub Desktop.
Save mcnemesis/5621370 to your computer and use it in GitHub Desktop.
A Bash script to help re-size a PostgreSQL VARCHAR column to a NEW SIZE, taking care to adjust for required meta-data space. This kind of script greatly helps in scenarios where an existing table (probably with tons of data already) suddenly becomes restricting for certain longer values to be inserted. You might also use it to re-size to a small…
#!/usr/bin/sh
if [ -z "$1" ]
then
echo "Usage: "$0" DBUSER DB TABLE COL NEWSIZE"
else
USER=$1
DB=$2
TABLE=$3
COL=$4
SIZE=$5
echo "Resizing COL ["$COL"] in TABLE ["$DB"."$TABLE"] to "$SIZE
psql $DB -U $USER <<SQL
SELECT atttypmod as "CURRENT_SIZE" FROM pg_attribute WHERE attrelid = '$TABLE'::regclass AND attname = '$COL';
UPDATE pg_attribute SET atttypmod = $SIZE+4 WHERE attrelid = '$TABLE'::regclass AND attname = '$COL';
SELECT atttypmod as "NEWSIZE" FROM pg_attribute WHERE attrelid = '$TABLE'::regclass AND attname = '$COL';
SQL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment