A simple Function for a hex grid
Github Repo: https://github.com/imran-5/Postgis-Custom
Function====================================================================
CREATE OR REPLACE FUNCTION public.I_Grid_Hex(geom geometry, radius double precision)
RETURNS SETOF geometry AS $BODY$
DECLARE
srid INTEGER := 4326;
input_srid INTEGER;
x_max DECIMAL;
y_max DECIMAL;
x_min DECIMAL;
y_min DECIMAL;
x_series DECIMAL;
y_series DECIMAL;
b float :=radius/2;
a float :=b/2; --sin(30)=.5
c float :=2*a;
--temp GEOMETRY := ST_GeomFromText(FORMAT('POLYGON((0 0, %s %s, %s %s, %s %s, %s %s, %s %s, 0 0))',
-- (b), (a), (b), (a+c), (0), (a+c+a), (-1*b), (a+c), (-1*b), (a)), srid);
geom_grid GEOMETRY := ST_GeomFromText(FORMAT('POLYGON((0 0, %s %s, %s %s, %s %s, %s %s, %s %s, 0 0))',
(radius * 0.5), (radius * 0.25),
(radius * 0.5), (radius * 0.75),
0 , radius,
(radius * -0.5), (radius * 0.75),
(radius * -0.5), (radius * 0.25)), srid);
BEGIN
CASE st_srid(geom) WHEN 0 THEN
geom := ST_SetSRID(geom, 4326);
RAISE NOTICE 'SRID Not Found.';
ELSE
RAISE NOTICE 'SRID Found.';
END CASE;
input_srid:=st_srid(geom);
geom := st_transform(geom, srid);
x_max := ST_XMax(geom);
y_max := ST_YMax(geom);
x_min := ST_XMin(geom);
y_min := ST_YMin(geom);
x_series := ceil ( @( x_max - x_min ) / radius);
y_series := ceil ( @( y_max - y_min ) / radius);
RETURN QUERY
With foo as(SELECT
ST_Translate ( cell, x*(2*a+c)+x_min, y*(2*(c+a))+y_min) AS hexa
FROM
generate_series ( 0, x_series, 1) AS x,
generate_series ( -1, y_series, 1) AS y,
(
SELECT ST_Translate(geom_grid::geometry, b , a+c) as cell
union
SELECT geom_grid AS cell
) AS foo
) select ST_CollectionExtract(ST_Collect(ST_Transform(ST_Intersection(ST_CollectionExtract(hexa, 3), geom), input_srid)),3)
from foo where ST_Intersects(hexa, geom);
END;
$BODY$ LANGUAGE 'plpgsql' VOLATILE;
Query for above question:
SELECT I_Grid_Hex(st_envelope(st_collect(geom)), 1) from "us-states"
Result

Another simple query
SELECT I_Grid_Hex(geom, .0001) from polygons limit 1
Result======================================================================
