Inserting polygon centroids into an additional column in PostGIS

If you want x and y columns:

ALTER TABLE "test_table" ADD x double precision;
ALTER TABLE "test_table" ADD y double precision;
UPDATE "test_table" SET x = ST_X(ST_Centroid(the_geom));
UPDATE "test_table" SET y = ST_Y(ST_Centroid(the_geom));

If you rather want a geometry column (adjust schema name and CRS to fit your needs):

SELECT AddGeometryColumn ('test_schema','test_table','centroid_geom',4326,'POINT',2);
UPDATE "test_table" SET centroid_geom = ST_Centroid(the_geom);

You must use ST_X function.

Assuming that you want to fill a column of a table that already has a geometry column with the x coordinate of the centroid you can use something like:

UPDATE test_table
SET x = ST_X(ST_Centroid(the_geom));

If you need something different post the complete structure of the table if you need more accurate help.


It is a Geometry (a Point in fact). What you are seeing is the text representation of the point as Well Known Binary (WKB) for debugging purposes you'd probably want well known text (WKT) (see https://postgis.net/docs/using_postgis_dbmanagement.html#OpenGISWKBWKT),

select
  ST_AsText(ST_Centroid(a.the_geom) )
FROM 
  "test_table" AS a

Will show what you want to see.

To do the addition you need a geometry column rather than a double. For this you need to use addGeometryColumn which handles all the bookkeeping needed for you (things like adding the column to the geometry columns metadata etc).