Spring Boot + Heroku Postgres

I struggled with the details, so I will write it down as a memo for myself.

It is assumed that Heroku Postgres has been added to the Heroku app. 2018-08-05_11h48_15.png

Spring Boot is 2.0.4.

Install the psql command on your local machine

Looking at the Heroku Potgres Settings (above), it says "** Heroku CLI **" and the following command is written.

heroku pg:psql datastore name--app app name

I got an error when I tried it in my environment. You need to put ** psql ** on your local machine. It's natural w

-->Connecting to datastore name
 !    The local psql command could not be located. For help installing psql, see
 !    https://devcenter.heroku.com/articles/heroku-postgresql#local-setup

Refer to URL above and go to your Windows 10 environment Windows Setup I installed it with articles / heroku-postgresql # set-up-postgres-on-windows). Then, I set the environment variable to "C: \ Program Files \ PostgreSQL \ \ bin".

heroku pg:psql datastore name--app app name
-->Connecting to datastore name
psql (10.4)
SSL connection(protocol: TLSv1.2, encryption method: ECDHE-RSA-AES256-GCM-SHA384, bit length:256, compression:off)
"help"Get help with.

app name::DATABASE=>

You have successfully connected.

command

This is a part that has nothing to do with Spring Boot or Heroku, but I made a note because I didn't know the Postgres command too much.

Database list display

\l

When I did this, a lot of databases came out, but I can see the DBs of other owners ...? Is it multi-tenant?

Database selection

\c database name
SSL connection(protocol: TLSv1.2, encryption method: ECDHE-RSA-AES256-GCM-SHA384, bit length:256, compression:off)
Database"xxxx"To the user"xxxx"Connected as.

Table list display

After creating a table with create table, display the table.

\d
Relationship list
Schema|name|Mold|owner
----------+----------+----------+----------------
 public   | nogizaka |table| xxxxxxxxxxxxx

Detailed display of table

See details such as columns.

\d nogizaka

The schema is super omission.

table"public.nogizaka"
Column|Mold|Collation|Null value allowed|Default
------+-----------------------+----------+---------------+------------
 id   | integer               |          |               |
 name | character varying(64) |          |               |

Spring Boot

Spring Initializr

Create a project template with Spring Initializr. I chose the following three dependencies. You don't have to have a web, but I've included it to make the runtime endpoint a REST API.

2018-08-06_18h58_47.png

application.properties

The contents of the connection to the database were defined and operated as follows based on the Database Credentials in the Settings of Heroku Postgres.

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://hostname:port number/Database name?sslmode=require
spring.datasource.username=User name
spring.datasource.password=password

When I first got hooked, the following two points were leaked.

--The URL "postgresql" was changed to "postgres" (I copied the URL written in the Heroku CLI) --I didn't add the sslmode parameter

code

All you have to do is write.

Entity

Nogizaka.java


package tech.kikutaro.herokupostgres;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="nogizaka")
public class Nogizaka {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;

    @Column(name="name")
    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

It may be better to use Lombok for setter and getter.

Repository

NogizakaRepository.java


package tech.kikutaro.herokupostgres;

import org.springframework.data.jpa.repository.JpaRepository;

public interface NogizakaRepository extends JpaRepository<Nogizaka, Long> {

}

Service

NogizakaService.java


package tech.kikutaro.herokupostgres;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NogizakaService {
    @Autowired
    NogizakaRepository nogirepo;

    public long count() {
        return nogirepo.count();
    }

    public Nogizaka getMember(long id) {
        return nogirepo.getOne(id);
    }
}

For the time being, just count and get.

Main

HerokupostgresApplication.java


package tech.kikutaro.herokupostgres;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HerokupostgresApplication {

	@Autowired
	private NogizakaService nogiService;

	public static void main(String[] args) {
		SpringApplication.run(HerokupostgresApplication.class, args);
	}

	@GetMapping("/count")
	public String count() {
		System.out.println(nogiService.count());
		return Long.toString(nogiService.count());
	}

	@GetMapping("/select/{id}")
	public String getName(@PathVariable("id") long id) {
		return nogiService.getMember(id).getName();
	}
}

Execution result

It worked safely.

http://localhost:8080/nogizaka/1

2018-08-09_23h07_44.png

http://localhost:8080/select/1

2018-08-10_00h00_18.png

Recommended Posts

Spring Boot + Heroku Postgres
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Spring boot memo writing (1)
First Spring Boot (DI)
SPRING BOOT learning record 02
Spring Boot2 cheat sheet
Spring Boot exception handling
Spring Boot Servlet mapping
Spring boot development-development environment-
Spring Boot learning procedure
Learning Spring Boot [Beginning]
Spring boot memo writing (2)
Spring Boot 2.2 Document Summary
[Spring Boot] DataSourceProperties $ DataSourceBeanCreationException
Spring Boot 2.3 Application Availability
Spring boot tutorials Topics
Download with Spring Boot
SerializationException in Spring Boot (1 series) + spring-security-oauth2 + Redis Session + Heroku
[Spring Boot] Environment construction (macOS)
Set context-param in Spring Boot
Try Spring Boot from 0 to 100.
Generate barcode with Spring Boot
Spring Boot on Microsoft Azure
Implement GraphQL with Spring Boot
Spring Boot tutorial task schedule
Spring 5 & Spring Boot 2 Hands-on preparation procedure
Get started with Spring boot
Hello World with Spring Boot!
Spring Boot 2 multi-project in Gradle
[Spring Boot] Web application creation
spring boot port duplication problem
Run LIFF with Spring Boot
SNS login with Spring Boot
Spring Boot Hot Swapping settings
[Java] Thymeleaf Basic (Spring Boot)
Introduction to Spring Boot ① ~ DI ~
File upload with Spring Boot
Spring Boot starting with copy
Introduction to Spring Boot ② ~ AOP ~
CICS-Run Java application-(4) Spring Boot application
Spring Boot starting with Docker
Spring Boot + Springfox springfox-boot-starter 3.0.0 Use
Spring Boot DB related tips
Hello World with Spring Boot
Set cookies with Spring Boot
[Spring Boot] Easy paging recipe
Use Spring JDBC with Spring Boot
Docker × Spring Boot environment construction
Major changes in Spring Boot 1.5
Add module with Spring Boot
Getting Started with Spring Boot
NoHttpResponseException in Spring Boot + WireMock
[Spring Boot] Send an email
Spring Boot performance related settings
Introduction to Spring Boot Part 1
Spring Boot External setting priority
Try using Spring Boot Security
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note