1. 程式人生 > >postgresql在liunx系統安裝部署測試

postgresql在liunx系統安裝部署測試

一、下載postgresql

wget https://ftp.postgresql.org/pub/source/v10.3/postgresql-10.3.tar.gz

二、下載zlib,因為安裝報錯

http://www.zlib.net/

類似的錯誤:

configure: error: readline library notfound
If you have readline already installed, see config.log for detailson the
failure.  It is possible the compiler isnt lookingin the proper directory.
Use --without-readline to disable readlinesupport.

三、安裝zlib

 tar zlib-1.2.11
 cd zlib-1.2.11/
 ./configure  
 make && make install

四、安裝postgresql

./configure --prefix=/home/postgreSql10 --without-readline
make && make install

五、建立postgres使用者與組

groupadd postgres
useradd -g postgres postgres
chown postgres:postgres /home/pgdatabase -R

六、修改環境變數

export PGHOME=/home/postgresql/postgresql-10.3
export PGDATA=/home/postgresql/postgresql-10.3/pgdatabase
export PATH=$PATH:$HOME/bin:/home/postgreSql10/bin
export PG_PATH=/home/postgreSql10/bin
export PATH=$PG_PATH:$PATH

在這裡插入圖片描述
加入後通過按 esc 按:wq儲存並退出,通過source /etc/profile

七、初始化資料庫

initdb -D  /home/postgresql/postgresql-10.3/pgdatabase   -E UTF-8 --locale=zh_CN.UTF-8

八、修改配置進行遠端連線
8.1、修改postgresql.conf
在這裡插入圖片描述在這裡插入圖片描述將行開頭都#去掉,將行內容修改為listen_addresses=’*'來允許資料庫伺服器監聽來自任何主機的連線請求。
8.2、修改pg_hba.conf
在這裡插入圖片描述在這裡插入圖片描述表示允許網段0.0.0.0上的所有主機使用所有合法的資料庫使用者名稱訪問資料庫,
md5並提供加密的密碼驗證

host    all             all             0.0.0.0/0               md5

trust意思不用密碼驗證

host    all             all             0.0.0.0/0              trust

九、啟動 PostgreSQL
先切換使用者
在這裡插入圖片描述啟動

 pg_ctl -D /home/super/soft/pgdatabase/ start

關閉

pg_ctl -D /home/super/soft/pgdatabase/ stop

重啟

pg_ctl -D /home/super/soft/pgdatabase/ reload

10、 檢視當前資料庫的狀態

ps -ef|grep postgres

11、連線資料庫

psql -p 5432 -U postgres -d postgres
create database test;

12、修改密碼

ALTER USER postgres with encrypted password '123456';

13、springboot+postgresql的演示
專案目錄
在這裡插入圖片描述
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.citydo</groupId>
    <artifactId>postgresql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>postgresql</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.properties配置

spring.datasource.url=jdbc:postgresql://127.0.0.1:54321/testscmdb?useSSL=false
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver

#解決java.sql.SQLFeatureNotSupportedException: 這個 org.postgresql.jdbc.PgConnection.createClob() 方法尚未被實作。
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

controller層

package com.citydo.postgresql.controller;


import java.util.List;
import com.citydo.postgresql.dao.UserRepository;
import com.citydo.postgresql.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    private UserRepository userRepository;


    @RequestMapping(value="/user")
    public List<User> userList(){
        return userRepository.findAll();
    }
}

dao層

package com.citydo.postgresql.dao;
import com.citydo.postgresql.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer>{
}

model層

package com.citydo.postgresql.model;

import javax.persistence.*;

@Entity
@Table(name="user", schema = "public")
public class User {

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

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

    @Column(name = "age")
    private  int age;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

大功告成
在這裡插入圖片描述
<參考:https://blog.csdn.net/yaoqiancuo3276/article/details/80212760>