Postgreqsl Query Error - postgresql

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.

Related

Rename and add column in same query in PostgreSQL

Let's say I have a table Student with just 2 columns - id bigint, name varchar(50).
I'm trying to rename it and add column in a same single query in PostgreSQL version 11.16 like below:
ALTER TABLE student
RENAME COLUMN name TO fullname,
ADD COLUMN roll_no varchar(30);
But I got this error:
Syntax error at or near "ADD"
Is it possible? If not, why?
Actually, that is not possible. I got this from the StackOverflow answer. One RENAME is possible for only one statement. Even multiple RENAME commands are also not possible. Here is the postgres manual.

"Attributes specified for column are incompatible with existing column definition"

It's been a while.
Using DB2 10 for z/OS, I've been asked to change a specific column in a table from decimal(7,2) to decimal(7,4). Sounds easy, right?
alter table MySchema.MyTable
alter column myColumn
set data type decimal(7,4);
But, DB2 responds with this error: "Attributes specified for column 'MYCOLUMN' are incompatible with existing column definition."
I had thought that converting from decimal(7,2) to decimal(7,4) would be pretty straightforward, but DB2 disagrees.
Outside of dropping the table and recreating it from scratch, what alternatives do I have?
Thanks in advance!
Dave
The reason Db2 doesn't like that change is you're going from from 99999.99 to 999.9999
Is that really what you want? Going from (7,2) to (9,4) would just add two more decimal places without losing any data and should be allowed by the Db.
Db2 for i gives a warning, but allows you to ignore the warning...
Create a new column ALTER ADD COLUMN of the right type, use an UPDATE to populate it, ALTER DROP COLUMN the old column. RENAME COLUMN so set the name of the original column.

ERROR: syntax error at or near "timestamp" when adding column

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.

How to rename a PostgreSQL table by prefixing an underscore?

I have a database which relies on a PostgreSQL system and I am maintaining it so I want to change tables and overall scheme. For this I thought of renaming the older tables so they have an underscore as a prefix. But this is not working:
DROP TABLE IF EXISTS _my_table; -- table does not exists, this does nothing
ALTER TABLE my_table
RENAME TO _my_table;
The result of the query is the following:
NOTICE: table "_my_table" does not exist, skipping ERROR:
type "_my_table" already exists
********** Error **********
ERROR: type "_my_table" already exists SQL state: 42710
The '_my_table' table is a fake name, but this error is reproduced by actually creating a '_my_table' table and running the same script above.
I am using pgAdmin III to access the database tables and making use of it's 'rename' operation results in the same error.
The postgresql documentation for the alter table method does not tell me explicitly about this particular problem: http://www.postgresql.org/docs/9.3/static/sql-altertable.html
Do I really need to use a prefix like 'backup' instead of '_' ? Or would it be possible to rename it, my only interest is to maintain the information in the table whilst having the minimal changes to the table name.
You cannot simply put an underscore in front of the existing table name because every table has an associated type that is... a leading underscore before the table name. You can verify this in the pg_catalog.pg_type table. Having a table name start with an underscore is not the problem, but the internal procedure is that a new table is created physically from the old table and only when the old table is no longer in use by other processes will the old table, and its associated type, be deleted. Hence the error referencing the type (and not the relation).
So if you really want to keep the old name with an underscore, you should first ALTER TABLE to some temp name and then ALTER TABLE to the underscore + original name. Or simply use another prefix...
ERROR: type "_my_table" already exists
Both tables and types are stored in the internal table pg_class. A unique name is required, that's why you get this error message.

Can't change datatype of column in postgres

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.