PostgreSQL ALTER TABLE: Add, Rename & Drop Columns
PostgreSQL ALTER TABLE explained for beginners: learn to add, rename, change and drop columns with simple copy-paste examples you can run in psql today.
PostgreSQL ALTER TABLE explained for beginners: learn to add, rename, change and drop columns with simple copy-paste examples you can run in psql today.
PostgreSQL ALTER TABLE is the command you use to change a table after you have created it. With it you can add a column, rename a column, change a data type, or drop a column you no longer need. In this beginner guide you will learn each one with small examples you can paste into psql and run.
A quick reminder of two words. A table is a grid that stores data, like a spreadsheet. A column is one field in that grid, such as a name or an email. If you have not made a table yet, read our guide on how to create a table in PostgreSQL first.
ALTER TABLE changes the structure of a table that already exists. It does not add or edit rows of data. Use UPDATE for that. Think of it as editing the column headings of your spreadsheet instead of the cells.
Here is the basic shape of the command. You write ALTER TABLE, then the table name, then the change you want.
ALTER TABLE table_name
action;The word action is a placeholder. In the next sections we will replace it with ADD COLUMN, RENAME COLUMN, and other real actions.
Let us create a small table called customers and put two rows in it. Run this in psql.
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT
);
INSERT INTO customers (name, email)
VALUES ('Ana', 'ana@example.com'),
('Ben', 'ben@example.com');The first statement makes a table with three columns. SERIAL gives each row a number automatically, and PRIMARY KEY makes that number unique. NOT NULL means the name can never be empty. The second statement adds two customers.
Say you now want to store a phone number. Use ADD COLUMN.
ALTER TABLE customers
ADD COLUMN phone TEXT;Line 1 says which table to change. Line 2 says to add a new column called phone that holds text. Existing rows get NULL in the new column, which means no value yet.
You can also give the new column a default value, so old rows are filled in for you.
ALTER TABLE customers
ADD COLUMN country TEXT DEFAULT 'Unknown';DEFAULT sets the value used when none is given. Now Ana and Ben both have the country Unknown. Check with SELECT * FROM customers;
If a name is unclear, you can change it. Here we rename phone to phone_number.
ALTER TABLE customers
RENAME COLUMN phone TO phone_number;RENAME COLUMN takes the old name, then TO, then the new name. The data stays exactly the same. Only the label changes.
You can also rename the whole table.
ALTER TABLE customers
RENAME TO clients;
ALTER TABLE clients
RENAME TO customers;The first statement renames the table to clients. The second one puts the name back so the rest of this guide still works.
A data type tells PostgreSQL what kind of value a column holds, such as text or a number. To change it, use ALTER COLUMN ... TYPE.
ALTER TABLE customers
ALTER COLUMN phone_number TYPE VARCHAR(20);VARCHAR(20) means text with at most 20 characters. If existing data cannot be converted, PostgreSQL shows an error and changes nothing. When converting text to a number you may need USING, for example ALTER COLUMN age TYPE INTEGER USING age::INTEGER.
You can also add a rule (called a constraint) to an existing column. Here we make sure email is always filled in, and then undo it.
ALTER TABLE customers
ALTER COLUMN email SET NOT NULL;
ALTER TABLE customers
ALTER COLUMN email DROP NOT NULL;SET NOT NULL forbids empty values. It fails if some rows already have NULL, so fix those first. DROP NOT NULL removes the rule again.
When you no longer need a column, remove it.
ALTER TABLE customers
DROP COLUMN country;DROP COLUMN deletes the column and all the data in it. There is no undo button, so be careful. If you want to skip the error when the column is missing, write DROP COLUMN IF EXISTS country.
| What you want | Command |
|---|---|
| Add a column | ALTER TABLE t ADD COLUMN c TEXT; |
| Rename a column | ALTER TABLE t RENAME COLUMN old TO new; |
| Rename a table | ALTER TABLE t RENAME TO new_t; |
| Change data type | ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(20); |
| Require a value | ALTER TABLE t ALTER COLUMN c SET NOT NULL; |
| Delete a column | ALTER TABLE t DROP COLUMN c; |
In PostgreSQL, ALTER TABLE changes can be run inside a transaction, which is a group of steps that either all work or all get cancelled. This makes experiments much safer.
Use ALTER TABLE table_name ADD COLUMN column_name data_type;. For example, ALTER TABLE customers ADD COLUMN phone TEXT; adds a text column called phone. Existing rows get NULL unless you set a DEFAULT.
Use ALTER TABLE table_name RENAME COLUMN old_name TO new_name;. The data in the column is not changed. Only the name is updated.
Most ALTER TABLE actions, such as adding or renaming a column, keep your data safe. DROP COLUMN removes the column and its data for good, and changing a type can fail or alter values. Always back up first.
Yes, if you run it inside a transaction. Type BEGIN;, run your ALTER TABLE, and then use ROLLBACK; to cancel or COMMIT; to save. Once committed, you must reverse the change by hand.
ALTER TABLE changes the structure of a table, like its columns. UPDATE changes the data stored in rows. Use ALTER TABLE for the headings and UPDATE for the cells.
Next, learn how to change the data inside your rows with our guide to updating and deleting data in PostgreSQL.
Read the UPDATE and DELETE guideNeed help building a database-backed app? Our team can guide you from first table to finished product.
Talk to FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.