Resizing a column in Postgres.

I was using hakanw’s django-email-usernames, to enable users to login with their email addresses as well. It was working great but it had only one issue. Django sets the character limit to 30 characters or less, for usernames. django-email-usernames does give the users the option to extend the username column when doing a fresh syncdb. But their sql command was not working for me for some reason. I think its because i was using postgres. 

So i had to manually log into postgres and resize the column. Here are some of my notes on how i did it.

If you issue the following query,

SELECT atttypmod FROM pg_attribute

WHERE attrelid = ‘auth_user’::regclass

AND attname = ‘username’;

It will give you the size of the column username. When i issued the command it gave me the output of 34, which means the size of the column is 30 since 4 is added for legacy reasons. To resize the username to 75, we use the following command.

UPDATE pg_attribute SET atttypmod = 75+4

WHERE attrelid = ‘auth_user’::regclass

AND attname = ‘username’;

 Another method as mentioned by my friend, @too_many_bryans, which also works just as fine is.

ALTER TABLE auth_user ALTER COLUMN username TYPE varchar(75)