Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the natural order of columns in Postgres?

Tags:

postgresql

Is it possible to change the natural order of columns in Postgres 8.1?

I know that you shouldn't rely on column order - it's not essential to what I am doing - I only need it to make some auto-generated stuff come out in a way that is more pleasing, so that the field order matches all the way from pgadmin through the back end and out to the front end.

like image 858
rjmunro Avatar asked Sep 24 '08 10:09

rjmunro


People also ask

How do I change the order of columns in PostgreSQL?

PostgreSQL currently defines column order based on the attnum column of the pg_attribute table. The only way to change column order is either by recreating the table, or by adding columns and rotating data until you reach the desired layout.

Does order of columns matter in Postgres?

The order of columns doesn't matter in creating tables in PostgreSQL, but it does matter sometimes in creating indexes in PostgreSQL.

How do you change the order of columns?

Press and hold the Shift key, and then drag the column to a new location. You will see a faint "I" bar along the entire length of the column and a box indicating where the new column will be moved. That's it! Release the mouse button, then leave the Shift key and find the column moved to a new position.

How do I change the default value of a column in PostgreSQL?

Changing a Column's Default Value. To set a new default for a column, use a command like: ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77; Note that this doesn't affect any existing rows in the table, it just changes the default for future INSERT commands.


1 Answers

You can actually just straight up change the column order, but I'd hardly recommend it, and you should be very careful if you decide to do it.

eg.

 # CREATE TABLE test (a int, b int, c int); # INSERT INTO test VALUES (1,2,3); # SELECT * FROM test;  a | b | c  ---+---+---  1 | 2 | 3 (1 row) 

Now for the tricky bit, you need to connect to your database using the postgres user so you can modify the system tables.

 # SELECT relname, relfilenode FROM pg_class WHERE relname='test';  relname | relfilenode  ---------+-------------  test_t  |       27666 (1 row)  # SELECT attrelid, attname, attnum FROM pg_attribute WHERE attrelid=27666;  attrelid | attname  | attnum  ----------+----------+--------     27666 | tableoid |     -7     27666 | cmax     |     -6     27666 | xmax     |     -5     27666 | cmin     |     -4     27666 | xmin     |     -3     27666 | ctid     |     -1     27666 | b        |      1     27666 | a        |      2     27666 | c        |      3 (9 rows) 

attnum is a unique column, so you need to use a temporary value when you're modifying the column numbers as such:

 # UPDATE pg_attribute SET attnum=4 WHERE attname='a' AND attrelid=27666; UPDATE 1 # UPDATE pg_attribute SET attnum=1 WHERE attname='b' AND attrelid=27666; UPDATE 1 # UPDATE pg_attribute SET attnum=2 WHERE attname='a' AND attrelid=27666; UPDATE 1  # SELECT * FROM test;  b | a | c  ---+---+---  1 | 2 | 3 (1 row) 

Again, because this is playing around with database system tables, use extreme caution if you feel you really need to do this.

This is working as of postgres 8.3, with prior versions, your milage may vary.

like image 104
Russell Avatar answered Sep 20 '22 09:09

Russell