Öncelikle bilgisayarımıza PostgreSQL istersek aşağıdaki linki kullanabiliriz.
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
PosstgreSQL’i yükledikten sonra pgadmin den server a nasıl bağlanırım?
Sol menüde Servers üzerine sağ tıkla → Create → Server… seç.
Name alanına istediğin bir isim yazabilirsin (örneğin: Local PostgreSQL
veya MyProjectDB ya da Mahmut de farketmez
).
Bu sadece pgAdmin’de görünecek isimdir, bağlantıyı etkilemez.


Daha sonra https://start.spring.io/ ‘ den Aşağıdaki dependency leri ekleyelim.
- Spring Web
- Spring Boot Dev Tools
- Lombok
- Rest Repositories
- Spring Data JPA
- PostgreSQL Driver

Uygulamamızın Dizin Yapısı:

model klasörü içinde Person.java doysası oluşturuyoruz.
— Person.java —-
package postgre.postgre.model;
import jakarta.persistence.*;
import lombok.Data;
@Data
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
repo klasörünün altında PersonRepo adında interface oluşturuyoruz.
— PersonRepo —-
package postgre.postgre.repo;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.jpa.repository.JpaRepository;
import postgre.postgre.model.Person;
@RepositoryRestResource
public interface PersonRepo extends JpaRepository<Person, Long> {
}
— PersonController.java –
package postgre.postgre;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import postgre.postgre.model.Person;
import postgre.postgre.repo.PersonRepo;
import static java.nio.file.Files.delete;
@RestController
public class PersonController {
@Autowired
private PersonRepo personRepo;
@PostMapping("/addPerson")
public Person addPerson(@RequestBody Person person) {
return personRepo.save(person);
}
@GetMapping("/getPersons")
public Iterable<Person> getPersons() {
return personRepo.findAll();
}
@PostMapping("/deletePerson")
public String deletePerson(@RequestBody Person person) {
personRepo.deleteById(person.getId());
return "Person deleted with id: " + person.getId();
}
@PostMapping("/updatePerson")
public Person updatePerson(@RequestBody Person person) {
Person existingPerson = personRepo.findById(person.getId()).orElse(null);
if (existingPerson != null) {
existingPerson.setName(person.getName());
return personRepo.save(existingPerson);
} else {
return null; // or throw an exception
}
}
}
—- application.properties —-
spring.application.name=postgre
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
POSTMAN i açıyoruz, localhost:8080/addPerson Post olarak tetiklediğimizde;

Tarayıcı ya http://localhost:8080/getPersons yazınca;

Tarayıcı ya http://localhost:8080/persons yazınca tüm kayıtlı verileri görebiliriz.

Tarayıcıya http://localhost:8080/persons/1 yazınca(yani 1. İndeks)

PostgreSQL de tablomuzu kontrol ettiğimizde gelen Kayıtları görebiliriz;

Tarayıcıya http://localhost:8080/getPersons Kayıtları tarayıcıda görebiliriz

Postman de silmek istediğimiz verinin id’sini yazıp localhost:8080/deletePerson tetiklediğimiz de;

PostgreSQL veritabanımız da aşağıdaki gibi olur. İd’ si 4 olan veri silinir.

id’ si 2 olan name değeri “Alper” olan veriyi POSTMAN de tetikleyerek değiştirebiliriz.Postman URL: localhost:8080/updatePerson


Github linki: https://github.com/ergunalper/Spring-Boot-ile-PostgreSQL-e-Ba-lant-Kurmak