---
# System prepended metadata

title: PostgreSQL 建置

---

建置PostgreSQL
```
sudo yum install --disablerepo=nexus-repo epel-release
```

添加PostgreSQL
```
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
```

安装PostgreSQL
```
sudo yum install -y --disablerepo=nexus-repo postgresql12 postgresql12-server
```

初始化資料庫並啟動PostgreSQL
```
sudo service postgresql initdb
sudo systemctl start postgresql
```

切換使用者
```
sudo -u postgres psql
```

創建db
```
CREATE DATABASE nexus;
```

替換密碼
```
CREATE USER nexus_user WITH ENCRYPTED PASSWORD 'your_password';
```
賦予權限
```
GRANT ALL PRIVILEGES ON DATABASE nexus TO nexus_user;
```
離開
```
\q
```

連線db nexus
```
psql -h 127.0.0.1 -U nexus -d nexus -W
```

如遇
```
psql: error: FATAL:  Ident authentication failed for user "nexus"
```

使用
```
sudo nano /var/lib/pgsql/12/data/pg_hba.conf
```
進去修改pg_hba.conf

![image](https://hackmd.io/_uploads/BJUCEqtH0.png)
將nexus加在最上面
```
host    nexus          nexus          127.0.0.1/32            md5
```

重新嘗試連接

```
psql -h 127.0.0.1 -U nexus -d nexus -W
```