Updated May 25, 2023
Introduction to PostgreSQL repository
PostgreSQL provides different repositories for different operating systems. The repository’s main use is maintaining the centralized database, which means a centralized location where we can store data and manage the database. PostgreSQL repository is helpful to integrate your current systems and help update or install multiple changes on existing applications and software tools on your computer. It also provides all supported updates or versions of PostgreSQL at all times. Managing applications and tools is very simple when we have a repository. PostgreSQL offers different types of repository systems to the user, like Apt and YUM.
Syntax
YUM
yum install postgresql_version
Explanation
We can install any postgresql version using the YUM repository in the above syntax. In this syntax, we use the YUM repository with the PostgreSQL version.
Similarly, we can install any application or tool using the YUM repository.
How does the repository work in PostgreSQL?
The working repository is the same on different types of operating systems, but the installation process is different, which means it depends on which operating system you have to use. Apart from the installation process, the remaining process is almost the same, and we must have the following basic knowledge.
- We must install PostgreSQL on your system.
- Install the PostgreSQL repository on your system.
- We must know PostgreSQL.
- We are also aware of PostgreSQL repositories.
- In order to access the PostgreSQL repository, we can install the Git hub.
Let’s see different types of PostgreSQL Repository as follows.
1. Apt Repository
The Apt support the current version of Ubuntu as follows
- 04
- 04
- 04
With the below architecture as follows
- amd64
- arm64 (18.04 and newer)
- i386 (18.04 and older)
- ppc64el
2. YUM Repository
This is a very common and popular repository for PostgreSQL. It supports the following operating system as follows.
- Linux Operating System/CentOS / Linux 8 Operating System of Oracle
- Linux Operating System /CentOS/ Linux Operating System of Oracle / Linux 7 Operating System
- Linux Operating System /CentOS/ Linux Operating System of Oracle / Linux 6 Operating System
- Fedora 32
- Fedora 31
With the below architecture as follows
- x86 (RHEL/CentOS/SL 6 only)
- x86_64
- ppcle64
- aarch64
Let’s see an example of a repository as follows.
Examples of PostgreSQL repository
We try to build Question and Answer applications by using the Java programming language with the PostgreSQL repository. In this example, we also use JPA and Hibernate with PostgreSQL.
First, boot the project by using the following command.
$ spring init --name=postgres-sample --dependencies=web,jpa,postgresql postgres-sample
Then start the spring by using the start,spring.io command.
Enter podtgres-sample.
Then add Web, JPA, and PostgreSQL in the dependencies section.
1. Configuration of PostgreSQL
First, configure spring boot to use PostgreSQL so you can add the database url, username, and password of PostgreSQL. Using the following path
src/main/resources/application.properties
Code:
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres_sample
spring.datasource.username= demo
spring.datasource.password=
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto = update
Explanation:
The first part of the code uses spring to show the DataSourceAutoConfiguration & DataSourceProperties of the application. dialect is used to select the best PostgreSQL database. Finally, we use hibernate to create databases, update databases, etc.
2. Configuration of Model
In this example, we implement two models as follows.
Audit Model: The AuditModel class is used to extend other entities.
Code:
package com.example.postgressample.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
value = {"createdAt", "updatedAt"},
allowGetters = true
)
public abstract class AuditModel implements Serializable {
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date createAt;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
@LastModifiedDate
private Date updatedAt;
}
3. Enable JPA Auditing
If a user needs to enable JPA Auditing, you must add @EnableJpaAuditing in the configuration class. So open the main class PostgressampleApplication.java and add this class.
Code:
package com.example.postgressample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@SpringBootApplication
@EnableJpaAuditing
public class PostgresSampleApplication {
public static void main(String[] args) {
SpringApplication.run(PostgresSampleApplication.class, args);
}
}
4. Quiz Model
It is mapped with quizzes in the database table as follows.
Code:
package com.example.postgressample.model;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
@Entity
@Table(name = " quizzes ")
public class Quiz extends AuditModel {
@Id
@GeneratedValue(generator = "quiz_generator")
@SequenceGenerator(
name = "quiz_generator",
sequenceName = "quiz_sequence",
initialValue = 1000
)
private Long id;
@NotBlank
@Size(min = 3, max = 100)
private String title;
@Column(columnDefinition = "text")
private String description;
}
5. Answer Model
In this class, we defined an answer class, and it mapped one to one relationship with the quiz model.
Code:
package com.example.postgressample.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
@Entity
@Table(name = "solution")
public class Ans extends AuditModel {
@Id
@GeneratedValue(generator = "ans_generator")
@SequenceGenerator(
name = "ans_generator",
sequenceName = "ans_sequence",
initialValue = 1000
)
private Long id;
@Column(columnDefinition = "text")
private String text;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "question_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
//@JsonIgnore private Quiz ;
}
6. Defining the Repositories
Following the repository, we used to access quizzes and solutions from the database.
QuizRepository: We can access the centralized quiz database using the following code in the Quiz repository.
Code:
package com.example.postgressample.repository;
import com.example.postgressample.model.Quiz;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface QuizRepository extends JpaRepository<Question, Long> {
}
Solution Repository: With the help of the solution repository, we get a solution for the quiz, so to create a solution repository, we use the following code.
Code:
package com.example.postgresample.repository;
import com.example.postgressample.model.Solution;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface SolutionRepository extends JpaRepository<Answer, Long> {
List<Answer> findByQuestionId(Long questionId);
}
After that, you must build a quiz question and solution controller using the API. We see here how we can create a repository using the PostgreSQL database.
Let’s see another simple example to understand the Postgresql repository.
Example:
In this application, we try to implement an online book management system by using a Java programming language; in this example, we try to use the postgreSQL repository for crud operation; let’s see
Code:
package com.demos.reactivepostgresql.data;
import com.demos.reactivepostgresql.models.Book_Details;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
public interface Book_DetailsRepository extends ReactiveCrudRepository<Book_Details, Long>
In this example, we use the above repository to develop online book management; here, only we see repository examples. Implementing the whole application here is impossible, so understand how to use the postgreSQL repository. The database table for the above repository looks as follows.
Uses
1. We can use a centralized database for the whole application or any software tool.
2. We can test code very easily when we use a repository.
3. It is much easier to communicate different components within the application.
4. The repository provides open access to users.
Conclusion
We hope from this article, you have understood the PostgreSQL Repository. From the above article, we have learned the basic example of the repository. We have also known how we can implement them with PostgreSQL. From this article, we have learned how we can handle PostgreSQL Repository.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL repository” was beneficial to you. You can view EDUCBA’s recommended articles for more information.