Created
May 21, 2013 16:56
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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