Easiest Way To Insert Scala Collection into PostgreSQL using Slick

Table of contents
Reading Time: 2 minutes

Few days ago, I had a scenario, in which I had to insert scala collection into postgreSQL using Slick. My postgreSQL table has some columns with data types such as Arrays, hstore  etc..
I tried to do this using slick, but didn’t get success. After beating my head whole day, I found a solution.
I found a slick extension slick-pg, which supports following postgreSQL types:-

  • Date/Time
  • Enum
  • Range
  • Hstore
  • LTree
  • JSON
  • Inet/MacAddr
  • text Search
  • postgis Geometry

In this blog, I have explained how to insert Scala collection into PostgreSQL using Slick through an example.  In this example,  there is a student table, which has 4 columns

Here hobbies is Array of String and marks is hstore(Set of key-value pair).

As suggested here https://github.com/tminglei/slick-pg , I created a trait MyPostgresDriver  and integrated with PostgresDriver like this:-

Then I mapped my table like this:-

After running command sbt run, I was able to store data in postgres.


For complete example, please go here https://gist.github.com/ayushmishra2005/d382801e0cb2d0df9e7c

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

4 thoughts on “Easiest Way To Insert Scala Collection into PostgreSQL using Slick2 min read

Comments are closed.