Ask Your Question
3

Is it possible to alter the name of a column in a PostgreSQL Function?

asked 2023-02-12 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-03-17 04:00:00 +0000

bukephalos gravatar image

Yes, it is possible to alter the name of a column in a PostgreSQL function using the ALTER TABLE statement. The syntax is as follows:

ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;

For example, if you have a function that creates a table with a column named "oldname", you can rename it to "newname" with the following code:

CREATE OR REPLACE FUNCTION create_table()
RETURNS VOID AS $$
BEGIN
    CREATE TABLE my_table (
        id SERIAL PRIMARY KEY,
        old_name TEXT
    );

    ALTER TABLE my_table RENAME COLUMN old_name TO new_name;
END;
$$ LANGUAGE plpgsql;
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-12 11:00:00 +0000

Seen: 10 times

Last updated: Mar 17 '22