How can I get all roads/ways from an osm2pgsql shema? - postgresql

I'm trying to create a list of every road in Germany (With latitude,longitude and Streetname).
So far I have imported the german osm file with the help of osm2pgsql into a postgres Database.
So what I am looking for is a query that allows me to query every road.

First of all I would suggest using the psql tool. So assuming your database is called gis enter the following command at the terminal:
psql gis
from within the psql tool you can query the database but first it is a g0od idea to get an idea of which tables were created by osm2psql, do this be entering:
\d
this should give you output like this:
List of relations
Schema | Name | Type | Owner
--------+--------------------+-------+----------
public | geography_columns | view | postgres
public | geometry_columns | table | gis
public | planet_osm_line | table | user
public | planet_osm_nodes | table | user
public | planet_osm_point | table | user
public | planet_osm_polygon | table | user
public | planet_osm_rels | table | user
public | planet_osm_roads | table | user
public | planet_osm_ways | table | user
public | spatial_ref_sys | table | gis
(10 rows)
There we can see a likely looking candidate in planet_osm_roads, enter:
\d planet_osm_roads
this will show you the structure of the roads table, which will look something like this:
Table "public.planet_osm_roads"
Column | Type | Modifiers
--------------------+----------+-----------
osm_id | bigint |
access | text |
addr:housename | text |
addr:housenumber | text |
addr:interpolation | text |
admin_level | text |
aerialway | text |
aeroway | text |
amenity | text |
....
waterway | text |
wetland | text |
width | text |
wood | text |
z_order | integer |
way_area | real |
way | geometry |
From there we can formulate our query. Something like:
SELECT osm_id,name,way FROM planet_osm_roads LIMIT 1;
will get you the first road in the list. The documentation at http://www.postgresql.org/docs/ will help you with decoding the way geometry string.

Related

SQL parameter table

I suspect this question is already well-answered but perhaps due to limited SQL vocabulary I have not managed to find what I need. I have a database with many code:description mappings in a single 'parameter' table. I would like to define a query or procedure to return the descriptions for all (or an arbitrary list of) coded values in a given 'content' table with their descriptions from the parameter table. I don't want to alter the original data, I just want to display friendly results.
Is there a standard way to do this?
Can it be accomplished with SELECT or are other statements required?
Here is a sample query for a single coded field:
SELECT TOP (5)
newid() as id,
B.BRIDGE_STATUS,
P.SHORTDESC
FROM
BRIDGE B
LEFT JOIN PARAMTRS P ON P.TABLE_NAME = 'BRIDGE'
AND P.FIELD_NAME = 'BRIDGE_STATUS'
AND P.PARMVALUE = B.BRIDGE_STATUS
ORDER BY
id
I want to produce 'decoded' results like:
| id | BRIDGE_STATUS |
|--------------------------------------|------------ |
| BABCEC1E-5FE2-46FA-9763-000131F2F688 | Active |
| 758F5201-4742-43C6-8550-000571875265 | Active |
| 5E51634C-4DD9-4B0A-BBF5-00087DF71C8B | Active |
| 0A4EA521-DE70-4D04-93B8-000CD12B7F55 | Inactive |
| 815C6C66-8995-4893-9A1B-000F00F839A4 | Proposed |
Rather than original, coded data like:
| id | BRIDGE_STATUS |
|--------------------------------------|---------------|
| F50214D7-F726-4996-9C0C-00021BD681A4 | 3 |
| 4F173E40-54DC-495E-9B84-000B446F09C3 | 3 |
| F9C216CD-0453-434B-AFA0-000C39EFA0FB | 3 |
| 5D09554E-201D-4208-A786-000C537759A1 | 1 |
| F0BDB9A4-E796-4786-8781-000FC60E200C | 4 |
but for an arbitrary number of columns.

postgREST can find relation

I'm trying to set up postgREST. Have been following the tutorial at http://postgrest.org/en/v5.1/tutorials/tut0.html. Here is what I see. First, the schemas:
entercarlson=# \dn
List of schemas
Name | Owner
--------+---------
api | carlson
public | carlson
Then a table:
carlson=# \d api.todos
Table "api.todos"
Column | Type | Collation | Nullable | Default
--------+--------------------------+-----------+----------+---------------------------------------
id | integer | | not null | nextval('api.todos_id_seq'::regclass)
done | boolean | | not null | false
task | text | | not null |
due | timestamp with time zone | | |
Indexes:
"todos_pkey" PRIMARY KEY, btree (id)
Finally, some data:
carlson=# select * from api.todos;
id | done | task | due
----+------+-------------------+-----
1 | f | finish tutorial 0 |
2 | f | pat self on back |
(2 rows)
But then I get this:
$ curl http://localhost:3000/todos
{"hint":null,"details":null,"code":"42P01","message":"relation
\"api.todos\" does not exist"}
Which is consistent with this:
carlson=# \d
Did not find any relations.
What am I doing wrong?
PS. I don't see which database this schema belongs to
Seems you're targeting the wrong database, check the db-uri config value and make sure this uri contains the api.todos table through psql.
Also, want to clarify that the search_path is modified by PostgREST on each request, so if you ALTER your connection user search_path it'll have no effect on the schemas PostgREST searches.

get Postgres UDT value with JOOQ

In Postgres, i created a UDT as follows
CREATE TYPE MY_TYPE AS ENUM ('FAILED', 'SUCCESS');
and in postgres cli, i can retrieve the UDT value back like
\dT+ MY_TYPE
which lists the details of the UDT, including its enum values under the 'Elements' column
EDIT: added result of "\dT+" command
postgres=# \dT+ status_type
List of data types
Schema | Name | Internal name | Size | Elements | Owner | Access privileges | Description
--------+-------------+---------------+------+----------+----------+-------------------+-------------
public | status_type | status_type | 4 | WAITING +| Postgres | |
| | | | STARTED +| | |
| | | | PAUSED +| | |
| | | | SUCCESS +| | |
| | | | FAILED | | |
Now, I am using JOOQ in a spring project, and want to get that UDT's value using JOOQ. Can anyone help me on how to get that done?
NOTE: I know that JOOQ generates the ENUM types when generating classes, But is that the only way to get the information i need?
Thanks!
I'm assuming you want to get a listing of all the enum literals from the database, dynamically. The SQL query that the jOOQ code generator uses for this similar to this:
SELECT n.nspname, t.typname, e.enumlabel, e.enumsortorder
FROM pg_enum e
JOIN pg_type t ON e.enumtypid = t.oid
JOIN pg_namespace n ON t.typnamespace = n.oid
ORDER BY n.nspname, t.typname, e.enumsortorder
Now, just run this query using jOOQ like any other query to get something like this (from the Sakila database):
nspname |typname |enumlabel |enumsortorder |
--------|------------|----------|--------------|
public |mpaa_rating |G |1 |
public |mpaa_rating |PG |2 |
public |mpaa_rating |PG-13 |3 |
public |mpaa_rating |R |4 |
public |mpaa_rating |NC-17 |5 |

Postgresql materialized view is refreshed by itself

I have this materialized view in my Postgres 9.4 database:
Materialized view "public.v_videolist"
Column | Type | Modifiers | Storage | Stats target | Description
----------+---------+-----------+----------+--------------+-------------
id | integer | | plain | |
title | text | | extended | |
embed | text | | extended | |
img | text | | extended | |
imgs | text | | extended | |
tags | text | | extended | |
category | text | | extended | |
vid | bigint | | plain | |
views | bigint | | plain | |
likes | bigint | | plain | |
unlikes | bigint | | plain | |
duration | integer | | plain | |
site | integer | | plain | |
Indexes:
"i_vl_id" UNIQUE, btree (id)
View definition:
SELECT videolist.id,
videolist.title,
videolist.embed,
videolist.img,
videolist.imgs,
videolist.tags,
videolist.category,
videolist.vid,
videolist.views,
videolist.likes,
videolist.unlikes,
videolist.duration,
videolist.site
FROM videolist
ORDER BY random();
Time to time this view refreshed by itself. There is no cron job to refresh it or something like that. It is just refreshed by itself from time to time, and I can't find who does it. I fully log all queries. There is no any refresh materialized view in the log.
Why is my view renewed? Any suggestions?
A job could be scheduled to update the statistics using this SQL statement:
REFRESH MATERIALIZED VIEW public.v_videolist;
You can use pg_cron to schedule the job.

How to load Chinook database in PostgreSQL?

Chinook database creation script contains all Western language characters in its text fields so I can't insert them in new PostgreSQL database set on UTF-8
PgAdmin III failed to load the script as well (using SQlite is fine).
The short answer to this is to use John Atten's version which is available here on his github. The rest of this is a short guide on how to run this.
The following assumes you have already installed and started an instance of PostgreSQL
Download the SQL Script
The official versions of the Chinook scripts can be found here. At time of writing, however, the standards for table and column names currently follow other standards. John Atten modified the official version to follow PostgreSQL standards, such as using the serial type, and it's available here. This is the version I used for this process.
Create the Database
Run a command to create a DB in which to store the chinook database. This one is named chinook for simplicity.
$ createdb chinook
$
Run the Downloaded File Using psql
psql can be used to run the creation code on the newly created database. If you are operating on a remote database, replace chinook with a connection string.
The -1 runs the entire file as a transaction, ensuring that you won't get a partially completed database on your server.
&>errorlog.txt will store the resulting output to the file errorlog.txt.
$ psql chinook -1 -f ~/Path/To/Download/chinook_pg_serial_pk_proper_naming.sql &>errorlog.txt
$
Test the DB to Ensure the Data Loaded
You should now check to see if the data loaded properly. You could use PGAdmin or Postico, but it can easily be checked from the command line using psql's \d command. Here is an example:
Once again, if connecting remotely, replace chinook with a connection string
$ psql chinook
psql (10.1)
Type "help" for help.
chinook=# \d
List of relations
Schema | Name | Type | Owner
--------+--------------------------------+----------+----------
public | actor | table | pmitdev1
public | actor_actor_id_seq | sequence | pmitdev1
public | album | table | pmitdev1
public | album_album_id_seq | sequence | pmitdev1
public | artist | table | pmitdev1
public | artist_artist_id_seq | sequence | pmitdev1
public | category | table | pmitdev1
public | category_category_id_seq | sequence | pmitdev1
public | customer | table | pmitdev1
public | customer_customer_id_seq | sequence | pmitdev1
public | employee | table | pmitdev1
public | employee_employee_id_seq | sequence | pmitdev1
public | film | table | pmitdev1
public | film_actor | table | pmitdev1
public | film_category | table | pmitdev1
public | film_film_id_seq | sequence | pmitdev1
public | genre | table | pmitdev1
public | genre_genre_id_seq | sequence | pmitdev1
public | invoice | table | pmitdev1
public | invoice_invoice_id_seq | sequence | pmitdev1
public | invoice_line | table | pmitdev1
public | invoiceline_invoiceline_id_seq | sequence | pmitdev1
public | media_type | table | pmitdev1
public | mediatype_mediatype_id_seq | sequence | pmitdev1
public | playlist | table | pmitdev1
public | playlist_playlist_id_seq | sequence | pmitdev1
public | playlist_track | table | pmitdev1
...