All posts
PostgreSQLOctober 16, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What does ALTER TABLE do?

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.

sql
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.

Set up a practice table

Let us create a small table called customers and put two rows in it. Run this in psql.

sql
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.

Add a column with ADD COLUMN

Say you now want to store a phone number. Use ADD COLUMN.

sql
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.

sql
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;

Rename a column with RENAME COLUMN

If a name is unclear, you can change it. Here we rename phone to phone_number.

sql
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.

sql
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.

Change a data type with ALTER COLUMN

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.

sql
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.

Add or remove rules on a column

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.

sql
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.

Delete a column with DROP COLUMN

When you no longer need a column, remove it.

sql
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.

Quick reference table

What you wantCommand
Add a columnALTER TABLE t ADD COLUMN c TEXT;
Rename a columnALTER TABLE t RENAME COLUMN old TO new;
Rename a tableALTER TABLE t RENAME TO new_t;
Change data typeALTER TABLE t ALTER COLUMN c TYPE VARCHAR(20);
Require a valueALTER TABLE t ALTER COLUMN c SET NOT NULL;
Delete a columnALTER TABLE t DROP COLUMN c;

Safety tips for beginners

  • Practice on a test database, not on data you care about.
  • Back up your data before using DROP COLUMN or changing a type.
  • Run SELECT * FROM table_name; first to see what is inside.
  • Use \d table_name in psql to view the current columns after each change.
  • You can wrap changes in BEGIN; and ROLLBACK; to test them safely. Use COMMIT; to keep them.

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.

Frequently asked questions

How do I add a column to a table in PostgreSQL?+

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.

How do I rename a column in PostgreSQL?+

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.

Does ALTER TABLE delete my data?+

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.

Can I undo ALTER TABLE in PostgreSQL?+

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.

What is the difference between ALTER TABLE and UPDATE?+

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 guide

Need help building a database-backed app? Our team can guide you from first table to finished product.

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.