Change column type and set not null - postgresql

How do you change the column type and also set that column to not null together?
I am trying:
ALTER TABLE mytable ALTER COLUMN col TYPE character varying(15) SET NOT NULL
This returns an error.
What is the right syntax?

This should be correct:
ALTER TABLE mytable
ALTER COLUMN col TYPE character varying(15),
ALTER COLUMN col SET NOT NULL

Also, if you want to REMOVE NOT NULL constrain in postgresql:
ALTER TABLE mytable
ALTER COLUMN email DROP NOT NULL;

Related

alter varchar field to super returns " ERROR: target data type "super" is not supported"

I have a varchar field in super/json format:
select detail
from mytable
limit 1;
{"child_category":"organize","gallery_id":"123456","detail":"[\"789876"]"}
I know that detail is currently varchar because inspecting the table in my client shows it as varchar(32768)
I want alter this field to be super:
ALTER TABLE mytable ALTER COLUMN detail TYPE super;
Returns:
[0A000] ERROR: target data type "super" is not supported
How can I cast the detail field as a super field?
From the doc https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html
with alter column you can only change varchar size.
As a suggestion, you can add new temp column, drop first, and rename temp column
alter table mytable add column temp_super super;
update mytable set temp_super = json_parse(detail);
alter table mytable drop column detail;
alter table mytable rename column temp_super to detail;

Alter column default value is not working in postgres

I tried to update column's default value with the below queries in postgres. But seems its not working. May be I am missing something. Could you help?
ALTER TABLE tableName ADD COLUMN newColumn INTEGER DEFAULT 0;
ALTER TABLE tableName ALTER COLUMN newColumn DROP DEFAULT;
or
ALTER TABLE tableName ALTER COLUMN newColumn SET DEFAULT NULL;
SELECT * FROM tableName;
Here I still find 0.
The change only applies to new records. After the modification you have to heal all the previous data with a migration like this:
UPDATE tableName SET newColumn = NULL WHERE newColumn = 0

Why default is not used when I set not null contratint?

I have table in postgresql database.
For given column I set default value, then I want it to be NOT NULL:
ALTER TABLE "order" ALTER COLUMN last_bill_date SET DEFAULT '-Infinity';
ALTER TABLE "order" ALTER COLUMN last_bill_date SET NOT NULL;
But second statement fails:
ERROR: column "last_bill_date" contains null values
Why DEFAULT value is not used when NOT NULL is applied for this column?
Per the documentation:
DEFAULT default_expr
(...)
The default expression will be used in any insert operation that does not specify a value for the column. If there is no default for a column, then the default is null.
The altered default expression cannot modify rows already existing in the table, you should do it before setting the not null constraint:
update "order"
set last_bill_date = '-Infinity'
where last_bill_date is null

Unable to change field type to not null with default value

I have a simple SQL statement, which looks like so:
alter table my_table alter column my_field set data type numeric(12,4) not null default 0;
But I get an error message, that points to not. What is wrong with that?
Use separate ALTER COLUMN clauses for the type, null behavior, and default value:
ALTER TABLE my_table
ALTER COLUMN my_field TYPE numeric(12,4),
ALTER COLUMN my_field SET DEFAULT 0,
ALTER COLUMN my_field SET NOT NULL;

PostgreSQL Text column to integer column

I have to convert one text column to integer column.
Showed code work when I have text (all numbers) in fields but now I find some empty strings '' where query stops.
ALTER TABLE mytable
ALTER COLUMN mycolumn TYPE integer USING (TRIM(mycolumn)::integer);
Can here be done so that empty strings be converted to integer 0 so this query will pass?
How to do this?
You can update your values before altering table:
UPDATE mytable SET mycolumn = '0' WHERE mycolumn = '';