{"id":2076,"date":"2015-12-30T09:40:19","date_gmt":"2015-12-30T14:40:19","guid":{"rendered":"http:\/\/springframework.guru\/?page_id=2076"},"modified":"2019-05-27T05:56:14","modified_gmt":"2019-05-27T09:56:14","slug":"configuring-spring-boot-for-postgresql","status":"publish","type":"post","link":"https:\/\/springframework.guru\/configuring-spring-boot-for-postgresql\/","title":{"rendered":"Configuring Spring Boot for PostgreSQL"},"content":{"rendered":"<p>Spring Boot makes it extremely convenient for programmers to quickly develop Spring applications using an in-memory database, such as <a title=\"H2 database Engine\" href=\"http:\/\/www.h2database.com\/html\/main.html\" target=\"_blank\" rel=\"noopener noreferrer\">H2<\/a>, <a title=\"HSQLDB (Hyper SQL Database)\" href=\"http:\/\/hsqldb.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">HSQLDB<\/a>, and <a title=\"Apache Derby\" href=\"https:\/\/db.apache.org\/derby\/\" target=\"_blank\" rel=\"noopener noreferrer\">Derby<\/a>. These databases are lightweight, easy to use, and emulates other RDBMS with the help of JPA and Hibernate. Obviously, they don\u2019t provide persistent storage; but they a fast\u00a0way to test persistent functions of your Spring Boot application without going through the hassles of installing a database server. They are great to use during development when you need to populate your database once your application starts, test your persistent entity mappings, and remove any data when your application ends. To use the embedded databases, you don\u2019t need any special configuration, not even any connection URL. If you are using Maven, you only specify the dependency of the database to use in the POM file. Spring Boot automatically sets up the in-memory database for your use when it finds the database on your classpath.<\/p>\n<p>In-memory databases are useful in the early development stages in local environments, but they have lots of restrictions. As the development progresses, you would most probably require an RDBMS to develop and test your application before deploying it to use a production database server, such as <a title=\"Oracle Database\" href=\"https:\/\/docs.oracle.com\/cd\/B19306_01\/server.102\/b14220\/intro.htm\" target=\"_blank\" rel=\"noopener noreferrer\">Oracle<\/a>, <a title=\"MySQL\" href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">MySQL<\/a>, or\u00a0<a title=\"PostgreSQL\" href=\"http:\/\/www.postgresql.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">PostgreSQL<\/a>.<\/p>\n<p>Previously, I wrote about creating a <a href=\"http:\/\/springframework.guru\/spring-boot-web-application-part-1-spring-initializr\/\" target=\"_blank\" rel=\"noopener noreferrer\">web application using Spring Boot<\/a> and also wrote about <a title=\"Configuring Spring Boot to use MySQL\" href=\"http:\/\/springframework.guru\/configuring-spring-boot-for-mysql\/\" target=\"_blank\" rel=\"noopener noreferrer\">configuring Spring Boot to use MySQL<\/a> for storing the application data.\u00a0In this post, we will learn how to change Spring Boot from the default in-memory H2 to PostgreSQL, which is one of the most advanced open source database that you will frequently see in production use.<\/p>\n<p><a href=\"http:\/\/www.postgresql.org\/\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2092 size-medium\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2015\/12\/postgresql-300x128.jpg\" alt=\"PostgreSQL\" width=\"300\" height=\"128\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2015\/12\/postgresql-300x128.jpg 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2015\/12\/postgresql.jpg 728w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>PostgreSQL Configuration<\/h2>\n<p>For this post, I&#8217;m using PostgreSQL running locally on my laptop. Let&#8217;s first start by creating a database for use with Spring Boot. You can do this by using the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">createdb<\/code> command line tool. You can locate this tool in the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">bin<\/code> folder of your PostgreSQL installation. To create a database named <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">springbootdb<\/code> open a command prompt\/terminal window and run the following command.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">createdb -h localhost -p 5432 -U postgres springbootdb\r\npassword *********\r\n<\/pre>\n<h2>PostgreSQL Dependency<\/h2>\n<p>To use PostgreSQL, you will need the proper database drivers. The required JARs are included in the Central Repository of Maven. To include them in your project, you need to add the following dependency (for PostgreSQL 9.2 and later) to your Maven POM file with your specific version.<\/p>\n<h4>POM.xml<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">...\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;org.postgresql&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;postgresql&lt;\/artifactId&gt;\r\n  &lt;version&gt;9.4-1206-jdbc42&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n...<\/pre>\n<p>For PostgreSQL up to 9.1, you need to add the following dependency with your specific version.<\/p>\n<h4>POM.xml<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">...\r\n&lt;dependency&gt;\r\n &lt;groupId&gt;postgresql&lt;\/groupId&gt;\r\n &lt;artifactId&gt;postgresql&lt;\/artifactId&gt;\r\n &lt;version&gt;9.1-901.jdbc4&lt;\/version&gt;\r\n&lt;\/dependency&gt;\r\n...<\/pre>\n<h2>Spring Boot Properties<\/h2>\n<p>Our example application is using the H2 database for development. Since H2 is on the classpath, Spring Boot will automatically provide us common sense defaults for the H2 datasource. But this is only if you do not specify another datasource. Thus, by simply providing properties for the PostgreSQL datasource we can override the H2 datasource.<\/p>\n<h4>application.properties<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">spring.jpa.hibernate.ddl-auto=create-drop\r\n<\/pre>\n<p>Since the example web application is using JPA, we configured Hibernate for PostgreSQL in Line 5 to use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">create-drop<\/code> option. This tells Hibernate to recreate the database on startup. However, in testing or production databases, you will\u00a0want to use the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">validate<\/code> option.<\/p>\n<figure id=\"attachment_4223\" aria-describedby=\"caption-attachment-4223\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/bit.ly\/2yhpu6x\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4223 size-full\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png\" alt=\"Spring Framework 5\" width=\"560\" height=\"292\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png 560w, https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png 300w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/a><figcaption id=\"caption-attachment-4223\" class=\"wp-caption-text\">Click Here to Become a Spring Framework Guru!<\/figcaption><\/figure>\n<h2>Running Spring Boot with PostgreSQL<\/h2>\n<p>Once you are done with the preceding configuration, Spring Boot becomes ready for use with PostgreSQL. When you start the project now, the Spring Boot application will use PostgreSQL for the database.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot makes it extremely convenient for programmers to quickly develop Spring applications using an in-memory database, such as H2, HSQLDB, and Derby. These databases are lightweight, easy to use, and emulates other RDBMS with the help of JPA and Hibernate. Obviously, they don\u2019t provide persistent storage; but they a fast\u00a0way to test persistent functions [&hellip;]<a href=\"https:\/\/springframework.guru\/configuring-spring-boot-for-postgresql\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4575,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Configuring Spring Boot for PostgreSQL #spring #springboot","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[104,1],"tags":[151,29],"class_list":["post-2076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring-boot","category-uncategorized","tag-postgresql","tag-spring-boot"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"Simanta","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/NewBannerBOOTSWeb.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-xu","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2076"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=2076"}],"version-history":[{"count":10,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2076\/revisions"}],"predecessor-version":[{"id":5198,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2076\/revisions\/5198"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4575"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=2076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=2076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=2076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}