Postgresql-13 安裝及數據目錄設置 - 收藏版

postgresql-13 的安裝

這篇文章的編寫時間是 postgresql13.1 之後發佈的,是最簡單粗暴的指導,沒有之一。
我已經幫大家踩完坑了,直接按步驟來就行了。

  1. 先更新安裝源,如下:
	sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
	sudo yum -y update
	sudo yum -y update
  1. 安裝 postgresql13
	sudo yum -y install postgresql13-server
  1. 初始化數據庫(如果想改變數據庫存儲位置,可以跳過這步)
	$ sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
	Initializing database ... OK
	#啓動服務
	$ systemctl start postgresql-13
	#允許服務開機運行
	$ systemctl enable postgresql-13

postgresql-13 修改初始化數據路徑

  1. 更改數據存儲位置

這裏介紹 systemctl 的使用如下:

    systemctl edit postgresql-13.service

在新空白頁覆蓋原服務配置

[Service]
Environment=PGDATA=/data/pgdata

這個文件默認放在 /etc/systemd/system/postgresql-13.service.d 目錄下。
裏面會有 override.conf 文件。

  1. 創建文件夾權限問題

當你執行這個命令時候,你會發現數據庫創建沒有返回‘OK’,我的天啊,而且直接閃退。

    $ /usr/pgsql-13/bin/postgresql-13-setup initdb

如果使用 systemctl start postgresql-13 命令的時候你會發現不好使。
其實際原因可以查看 more /var/lib/pgsql/13/initdb.log 的日誌,你會發現如下錯誤:

    Data page checksums are disabled.
    fixing permissions on existing directory /data/pgdata ... initdb: error: could not change permissions of directory "/data/pgdata": Operation not permitted
    The files belonging to this database system will be owned by user "postgres".
    This user must also own the server process.

意思是指這個文件夾不是 postgres 創建的,所以你要通過 postgres 創建這個文件夾,就可以了。

    $ su - postgres
    $ cd /data
    $ mkdir ./pgdata

然後你在試下:

    $ /usr/pgsql-13/bin/postgresql-13-setup initdb
    Create Database.
    Ok.
    systemctl start postgresql-13.service
    systemctl enable postgresql-13.service

貌似沒有錯誤了。

  1. 修改 postgres 默認密碼

    # 切換到 postgres 用戶
    $ sudo -u postgres psql
    psql (13.1)
    Type "help" for help.

    postgres=# ALTER USER postgres WITH PASSWORD ‘postgres’;
  1. 修改登錄方式

首先修改 /var/lib/pgsql/10/data/postgresql.conf

    listen_address = '*'
    
    password_encryption = 'md5'

在修改 /var/lib/pgsql/10/data/pg_hba.conf, 如下

    # TYPE  DATABASE        USER            ADDRESS                 METHOD

    # "local" is for Unix domain socket connections only
    local   all             all                                     peer
    # IPv4 local connections:
    #host    all             all             127.0.0.1/32            scram-sha-256
    host    all             all             0.0.0.0/0               md5
    # IPv6 local connections:
    host    all             all             ::1/128                 ident
    # Allow replication connections from localhost, by a user with the
    # replication privilege.
    local   replication     all                                     peer
    host    replication     all             127.0.0.1/32            ident
    host    replication     all             ::1/128                 ident

但是我發現,nodejs 連接 postgresql-13 是好使了,但是 navicate 不行,原因是 postgresql-13 改動還是不小的,安全加密策略有變化和完善,現在已經使用 scram-sha-256 方式了。

沒招把 md5 改爲 password 方式就 ok 了,也都正常了。如下:

    # TYPE  DATABASE        USER            ADDRESS                 METHOD

    # "local" is for Unix domain socket connections only
    local   all             all                                     peer
    # IPv4 local connections:
    #host    all             all             127.0.0.1/32            scram-sha-256
    host    all             all             0.0.0.0/0               password
    # IPv6 local connections:
    host    all             all             ::1/128                 ident
    # Allow replication connections from localhost, by a user with the
    # replication privilege.
    local   replication     all                                     peer
    host    replication     all             127.0.0.1/32            ident
    host    replication     all             ::1/128                 ident
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://blog.csdn.net/uucckk/article/details/111519646