Skip to content
Advertisement

How to change the position of column in postgresql without dumping

I know the only workaround is to dump the table and then recreate the whole database with correct positioning of the required columns.

But the problem is one of the columns is a foreign key to many tables in the database so it would not be possible to just delete the whole table. Also I cannot delete any column as the foreign key column lies to the last of the table. Please give me a solution.

If this question is a duplicate please give me the link to the correct answer.

edit: for more clarification i want to add rows using the insert command and the problem is the second last column is of the type serial. My main aim is to not touch the serial column while giving the insert command

Advertisement

Answer

All you need is a custom insert statement,

For example,

Your table looks like this,

CREATE TABLE "public"."ada" (
    
    "trandate" date, 
    "locname" text, 
    "totusers" integer, 
    "actusers" integer, 
    "datausage" integer, 
    "issues" integer, 
    "id" serial PRIMARY KEY, 
    "issuessolved" integer
);

and the insert statement can be written as

INSERT INTO "public"."ada" (
    "trandate"
    ,"locname"
    ,"totusers"
    ,"actusers"
    ,"datausage"
    ,"issues"
    ,"issuessolved"
    )
VALUES (
    < trandate
    ,date >
    ,< locname
    ,text >
    ,< totusers
    ,integer >
    ,< actusers
    ,integer >
    ,< datausage
    ,integer >
    ,< issues
    ,integer >
    ,< issuessolved
    ,integer >
    );
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement