How to load Chinook database in PostgreSQL? - 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
...

Related

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.

PostgreSQL: accessing relation with "public" schema name [duplicate]

This question already has answers here:
I keep getting the error "relation [TABLE] does not exist"
(1 answer)
Cannot simply use PostgreSQL table name ("relation does not exist")
(18 answers)
Setting schema in PostgreSQL JDBC doesn't seem to work
(1 answer)
Closed 4 years ago.
Using PostgreSQL 9.6, I created new table using pgAdmin under public schema.
But to access the table, I can't use just the table name. If I use public."Book" then only the table is accessible.
thrillio=# show search_path;
search_path
-------------
public
(1 row)
\d+ shows all the tables..
thrillio=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+-------------------+-------+----------+------------+-------------
public | Book | table | postgres | 8192 bytes |
public | KidFriendlyStatus | table | postgres | 0 bytes |
public | Movie | table | postgres | 8192 bytes |
public | User | table | postgres | 8192 bytes |
public | Weblink | table | postgres | 8192 bytes |
(5 rows)
thrillio=# select * from Book;
ERROR: relation "book" does not exist
LINE 1: select * from Book;
^
thrillio=# select * from public."Book";
id | title | profileUrl | publicationYear | publisher | authors | genre | amazonRating
----+-------+------------+-----------------+-----------+---------+-------+--------------
(0 rows)
is this expected? Anything I need to change the search_path to fix this?

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 |

How do I use an extension installed in a different schema?

I added an extension and my install shows the module installed,
mydb=# \dx
List of installed extensions
Name | Version | Schema | Description
--------+---------+------------+-------------------------------------------------------------------
pg_trgm | 1.0 | extensions | text similarity measurement and index searching based on trigrams
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
(2 rows)
But when I go to use it I'm getting an error,
mydb=# select similarity('hello','hell');
ERROR: function similarity(unknown, unknown) does not exist
LINE 1: select similarity('hello','hell');
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Is there any other step to use the functions the extension provides?
The module was installed into the schema extensions. I guess that that schema is not in your search path and therefor the function is not found. Try
select extensions.similarity('hello','hell');
If you don't want to fully-qualify the function names you can simply add that schema to your search_path:
alter user your_pg_user set search_path = public,extensions;
If that still gives an error your Postgres user might not have the necessary privileges to call the functions. In that case log in as the superuser (typically postgres) and grant the priviliges:
grant all on schema extensions to your_pg_user;
It should work. Here a psql protocol: (sorry, system is in german)
str#s131-intel:~> psql
psql (9.2.7)
strobel=# CREATE EXTENSION pg_trgm;
CREATE EXTENSION
strobel=# \df
Liste der Funktionen
Schema | Name | Ergebnisdatentyp | Argumentdatentypen | Typ
--------+------------------------+------------------+---------------------------------------------------------------------------+--------
public | flipper | boolean | | normal
public | gin_extract_query_trgm | internal | text, internal, smallint, internal, internal, internal, internal | normal
public | gin_extract_value_trgm | internal | text, internal | normal
<cut>
public | gtrgm_union | integer[] | bytea, internal | normal
public | set_limit | real | real | normal
public | show_limit | real | | normal
public | show_trgm | text[] | text | normal
public | similarity | real | text, text | normal
public | similarity_dist | real | text, text | normal
public | similarity_op | boolean | text, text | normal
(20 Zeilen)
strobel=# select similarity('hallo', 'hello');
similarity
------------
0.333333
(1 Zeile)
The error message gives a hint that your parameters were not seen as text, so maybe you wrote a backtick (accent grave) instead of apostrophe.

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

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.