[PostgreSQL] ALTER TABLE

After importing the raw CSV files into PostgreSQL, I noted that many of the columns were set to generic data types like varchar(50)
or even just text
. Totally normal, but definitely not ideal if you’re planning to run serious analysis.
What does ALTER TABLE
do?
In SQL, ALTER TABLE
lets you modify the structure of a table without having to recreate it from scratch.
You can use ALTER TABLE to
change column data types
Rename columns
Add or remove columns
Even rename the entire table
Why it matters in Business Intelligence?
Let’s say you have a column called date
, but it’s been imported as plain text
(or varchart
). If you want to filter by month or calculate time-based trends, you’ll need that to be an actual DATE type.
For instance,
ALTER TABLE invoice_raw
ALTER COLUMN date TYPE DATE USING date::DATE;
The USING
clause is super important here; it tells PostgreSQL how to convert the existing values. Without it, the database would throw an error because it doesn’t magically know how to turn text into dates.
On top of that..
I used similar commands to clean up columns like
amount
,price
,balance
column: changed to NUMERIC data typeappt_date
,date_created
: changed to DATE data typephone_number
: changed to TEXT (because phone number can break if treated as integers, and they often exceed the digit limit for the INTEGER type)
These small tweaks make a huge difference.
Now my data is easier to work with, my queries run faster, and I won’t get weird bugs when I try to aggregate things by date or sum up prices.
Subscribe to my newsletter
Read articles from kyuwon kang directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
