|
| 1 | +create table geoip2_network ( |
| 2 | + network cidr not null, |
| 3 | + geoname_id int, |
| 4 | + registered_country_geoname_id int, |
| 5 | + represented_country_geoname_id int, |
| 6 | + is_anonymous_proxy bool, |
| 7 | + is_satellite_provider bool, |
| 8 | + postal_code text, |
| 9 | + latitude numeric, |
| 10 | + longitude numeric, |
| 11 | + accuracy_radius int |
| 12 | +); |
| 13 | +create index on geoip2_network using gist (network inet_ops); |
| 14 | + |
| 15 | +create table geoip2_location ( |
| 16 | + geoname_id int not null, |
| 17 | + locale_code text not null, |
| 18 | + continent_code text not null, |
| 19 | + continent_name text not null, |
| 20 | + country_iso_code text, |
| 21 | + country_name text, |
| 22 | + subdivision_1_iso_code text, |
| 23 | + subdivision_1_name text, |
| 24 | + subdivision_2_iso_code text, |
| 25 | + subdivision_2_name text, |
| 26 | + city_name text, |
| 27 | + metro_code int, |
| 28 | + time_zone text, |
| 29 | + is_in_european_union bool not null, |
| 30 | + primary key (geoname_id, locale_code) |
| 31 | +); |
| 32 | + |
| 33 | +ALTER TABLE "public"."query" ALTER COLUMN "user_host_address" TYPE cidr using "user_host_address"::cidr; |
| 34 | + |
| 35 | +CREATE OR REPLACE VIEW v_geolocation AS ( |
| 36 | + WITH user_host_address AS ( |
| 37 | + SELECT |
| 38 | + q.text, |
| 39 | + q.user_host_address, |
| 40 | + q.created_date |
| 41 | + FROM |
| 42 | + query q |
| 43 | + WHERE |
| 44 | + q.user_host_address IS NOT NULL |
| 45 | + ORDER BY q.created_date DESC |
| 46 | + LIMIT 1000) |
| 47 | + SELECT |
| 48 | + uha.text, |
| 49 | + uha.user_host_address, |
| 50 | + uha.created_date, |
| 51 | + net.network, |
| 52 | + net.geoname_id, |
| 53 | + net.registered_country_geoname_id, |
| 54 | + net.represented_country_geoname_id, |
| 55 | + net.is_anonymous_proxy, |
| 56 | + net.is_satellite_provider, |
| 57 | + net.postal_code, |
| 58 | + net.latitude, |
| 59 | + net.longitude, |
| 60 | + net.accuracy_radius, |
| 61 | + loc.locale_code, |
| 62 | + loc.continent_code, |
| 63 | + loc.continent_name, |
| 64 | + loc.country_iso_code, |
| 65 | + loc.country_name, |
| 66 | + loc.subdivision_1_iso_code, |
| 67 | + loc.subdivision_1_name, |
| 68 | + loc.subdivision_2_iso_code, |
| 69 | + loc.subdivision_2_name, |
| 70 | + loc.city_name, |
| 71 | + loc.metro_code, |
| 72 | + loc.time_zone, |
| 73 | + loc.is_in_european_union |
| 74 | + FROM |
| 75 | + user_host_address uha |
| 76 | + JOIN geoip2_network net ON (net.network >> uha.user_host_address) |
| 77 | + JOIN geoip2_location loc ON (net.geoname_id = loc.geoname_id |
| 78 | + AND loc.locale_code = 'en')); |
0 commit comments