# Alter table and add new column if already exists

**URL:** https://community.cratedb.com/t/alter-table-and-add-new-column-if-already-exists/1748
**Category:** CrateDB
**Tags:** sql
**Created:** [April 2, 2024, 12:55pm UTC](https://community.cratedb.com/t/alter-table-and-add-new-column-if-already-exists/1748 "2024-04-02T12:55:47Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Alin\_Mihut](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/alin_mihut/32/695_2.png) [@Alin\_Mihut](https://community.cratedb.com/u/Alin_Mihut)
#### Post date: [April 2, 2024, 12:55pm UTC](https://community.cratedb.com/t/alter-table-and-add-new-column-if-already-exists/1748/1 "2024-04-02T12:55:47Z")

</div>

Hello,

It seems that the IF NOT EXISTS condition is not supported in the ALTER TABLE statement

Is there an alternative to execute such statement as below?

ALTER TABLE table\_name ADD COLUMN IF NOT EXISTS month AS date\_trunc(‘month’, timestamp) GENERATED ALWAYS;

---

<div class="post-metadata">

### Author: ![proddata](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/proddata/32/1379_2.png) [@proddata](https://community.cratedb.com/u/proddata)
#### Post date: [April 3, 2024, 8:04am UTC](https://community.cratedb.com/t/alter-table-and-add-new-column-if-already-exists/1748/2 "2024-04-03T08:04:29Z")

</div>

Hi @Alin_Mihut

No, there is no direct alternative other than manual checking beforehand.  
If you see this as an important feature feel free to open a feature request in `crate/crate`:

> **[GitHub - crate/crate: CrateDB is a distributed and scalable SQL database for...](https://github.com/crate/crate)**
>
> CrateDB is a distributed and scalable SQL database for storing and analyzing massive amounts of data in near real-time, even with complex queries. It is PostgreSQL-compatible, and based on Lucene. ...

Anyway it is not possible to add generated columns to a table that already holds data:

```sql
CREATE TABLE t001 (ts TIMESTAMP);
-- CREATE OK, 1 row affected (0.103 sec)
ALTER TABLE t001 ADD COLUMN ts_g GENERATED ALWAYS AS date_trunc('day',ts);
-- ALTER OK, -1 rows affected (0.076 sec)
INSERT INTO t001 (ts) VALUES (now());
-- INSERT OK, 1 row affected (0.055 sec)
ALTER TABLE t001 ADD COLUMN ts_g2 GENERATED ALWAYS AS date_trunc('week',ts);
-- UnsupportedFeatureException[Cannot add a generated column to a table that isn't empty]

```

best regards  
Georg
