I'm trying to create a column named geo that has a column that combines two randomly generated points. My table looks like this.
create table data (
id SERIAL primary key,
first VARCHAR(50),
last VARCHAR(50),
g VARCHAR(50),
geo VARCHAR(100)
);
Here is my try at generating those random values but get an error because the INSERT has more expression than target columns
INSERT INTO data (first,last,g,geo)
SELECT substr(md5(random()::text), 1, 7),
substr(md5(random()::text), 1, 10),
substr(md5(random()::text), 1, 1),
(random() * (47.606209 - 25.427152)) + 25.427152 , (random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);
Does anyone know how to do this? Also, I know geo locations shouldn't be strings but in my case it works perfectly. Thanks
The comma between the values indicates they are two different columns.
INSERT INTO data (first,last,g,geo)
SELECT
-- first
substr(md5(random()::text), 1, 7),
-- last
substr(md5(random()::text), 1, 10),
-- g
substr(md5(random()::text), 1, 1),
-- geo
(random() * (47.606209 - 25.427152)) + 25.427152,
-- ?fifth column?
(random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);
If you want to concatenate strings use ||
.
INSERT INTO data (first,last,g,geo)
SELECT
-- first
substr(md5(random()::text), 1, 7),
-- last
substr(md5(random()::text), 1, 10),
-- g
substr(md5(random()::text), 1, 1),
-- geo
(random() * (47.606209 - 25.427152)) + 25.427152 ||
',' ||
(random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);
However, storing these two numeric values as a string will make it slow and difficult to use. Instead, use two numeric
columns to store the two numbers.
create table data (
id bigserial primary key,
-- Don't put artificial limits on columns, it doesn't save space.
-- They'll only use as much space as necessary.
-- Use `text` unless there's a good reason for a limit.
-- In Postgres, `text` is just an unlimited `varchar`.
first text,
last text,
g text,
geox numeric,
geoy numeric
);
INSERT INTO data (first,last,g,geox,geoy)
SELECT
-- first
substr(md5(random()::text), 1, 7),
-- last
substr(md5(random()::text), 1, 10),
-- g
substr(md5(random()::text), 1, 1),
-- geox
(random() * (47.606209 - 25.427152)) + 25.427152,
-- geoy
(random() * (-124.389641 - -69.082237)) + -69.082237
FROM generate_series(1, 20);
Now they can be compared as numbers and they can be formatted as you need.
select geox || ',' || geoy from data;
Better yet, use the built inpoint
type .
create table data (
id bigserial primary key,
first text,
last text,
g text,
geo point
);
These can also be generated as x,y
strings and cast to a point
.
select (random() || ',' || random())::point;
And Postgres has plenty ofbuilt in 2-D geometric functions .
If you want to do real GIS, use PostGIS .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.