I am trying to add a column to a table and the following is my command:
alter table history add column start type timestamp;
I get the following error:
syntax error at or near "timestamp"
How is this command incorrect?
Remove the word type, it should be:
alter table history add column start timestamp
Also, start is a non-reserved keyword in PG (but reserved in the SQL99 standard) and as such might not be a good column name.
Related
I'm trying to rollback an addition that was made to this Enum named group_type in Postgres that was made to a column named type on my table groups.
In Postgress I've run,
ALTER TYPE group_type RENAME TO group_type_old;
CREATE TYPE group_type AS ENUM ('public', 'private');
ALTER TABLE groups ALTER COLUMN type DROP DEFAULT;
ALTER TABLE groups ALTER COLUMN "type" TYPE group_type USING "type"::text::group_type;
And I get the following error on the last statement,
Query 1 ERROR: ERROR: operator does not exist: text = group_type_old
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Any ideas what the issue is with my last statement?
You code works as intended. Even no problem in Postgresql 9.6!
Postgresql 14 demo
Postgresql 9.6 demo
I generally wrapped it in a transaction.
begin;
ALTER TYPE group_type RENAME TO group_type_old;
CREATE TYPE group_type AS ENUM ('sad', 'ok','happy','happy1');
ALTER TABLE groups ALTER COLUMN type DROP DEFAULT;
ALTER TABLE groups ALTER COLUMN "type" TYPE group_type USING "type"::text::group_type;
drop type group_type_old;
commit;
For what it's worth; I ran into this error when a value from the old enum was still mentioned as a CHECK constraint in my table, which also had the enum in a column.
I solved it by dropping the constraint, changing the enum, then adding the (updated) constraint again.
I'm not super experienced at this, but I have a column which now houses stings which need to be altered to dates. It's giving me an error for when there are only empty strings. Here's what's up.
ALTER TABLE artist
ALTER COLUMN available_from TYPE date USING available_from::date;
This is giving me the following error:
invalid input syntax for type date: ""
Then I tried
ALTER TABLE artist
ALTER COLUMN available_from TYPE date USING (NULLIF(available_from),'')::date,
Which gave me:
syntax error at or near ")"
Anyone who can help me out with this?
You misplaced the ) in the second statement:
ALTER TABLE artist
ALTER COLUMN available_from TYPE date
USING (NULLIF(available_from,''))::date,
^
| here
I'm running Postgres 8.4.13, and trying to add a constraint to an existing table. According to the docs, this should be possible:
alter table indexed_friends add constraint no_duplicate_user_friends unique (user, friend);
Yet when I run this I get the following error:
ERROR: syntax error at or near "user"
I'm confused because I'm following an unique constraint example listed in the documentation almost exactly. I can provide the table schema, but since it's complaining about a syntax error, I'm not sure that's necessary.
Ahhh... The word user is a reserved word in Postgres.
Surrounding it in quotes:
alter table indexed_friends add constraint no_duplicate_user_friends unique ("user", friend);
worked.
I'm running the following query and it's saying that it is not valid:
redmine=# ALTER TABLE changesets ALTER COLUMN committer TYPE character varying(100);
ERROR: syntax error at or near "TYPE" at character 47
Does anyone have any idea on the solution to this issue?
Postgres 7.4 does not support retyping columns. You have to create a new column, copy over any data, drop the old column and rename the new to have the old name.
If there is no data to copy, you can of course simply drop the old column and create the new column with the new type.
I'm trying to change the datatype of a column to varchar but get the following error
ERROR: syntax error at or near "type" at character 40
My code looks as follows
alter table n_logs alter column action type varchar(100);
I'm running PostgreSQL 7.4.13 (Yeah, I know I need to upgrade)
I don't think you can do that:
http://www.postgresql.org/docs/7.4/interactive/ddl-alter.html
You should split it into 3 steps
add new column
copy values from 1st column to 2nd
drop old column
I know you said you already found a solution, but your command would have worked if you took out the "action" keyword, just like this:
ALTER TABLE table_name_here
ALTER COLUMN column_name_here type varchar(100);
The SQL above worked for me, thanks.