Postgresql安裝教學
安裝的指令如下
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %rhel)-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum -qy module disable postgresql
sudo yum install -y postgresql13-server postgresql13
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
sudo -i -u postgres
psql
接著建立資料庫
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb;
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
退出命令行
\q
exit
安裝PgAdmin
以下為安裝的指令建議
$ sudo mkdir /var/lib/pgadmin
$ sudo mkdir /var/log/pgadmin
$ sudo chown $USER /var/lib/pgadmin
$ sudo chown $USER /var/log/pgadmin
$ python3 -m venv pgadmin4
$ source pgadmin4/bin/activate
(pgadmin4) $ pip install pgadmin4
...
(pgadmin4) $ pgadmin4
NOTE: Configuring authentication for SERVER mode.
Enter the email address and password to use for the initial pgAdmin user account:
Email address: user@domain.com
Password:
Retype password:
Starting pgAdmin 4. Please navigate to http://127.0.0.1:5050 in your browser.
* Serving Flask app "pgadmin" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
強烈建議使用 Python 虛擬環境,不然因為牽涉到的library眾多,很容易會有函式庫之間的彼此衝突
安裝及啟動pgAdmin的方法
以下為安裝方式
$ sudo mkdir /var/lib/pgadmin
$ sudo mkdir /var/log/pgadmin
$ sudo chown $USER /var/lib/pgadmin
$ sudo chown $USER /var/log/pgadmin
$ python3 -m venv pgadmin4
$ source pgadmin4/bin/activate
(pgadmin4) $ pip install pgadmin4
...
(pgadmin4) $ pgadmin4
NOTE: Configuring authentication for SERVER mode.
Enter the email address and password to use for the initial pgAdmin user account:
Email address: user@domain.com
Password:
Retype password:
Starting pgAdmin 4. Please navigate to http://127.0.0.1:5050 in your browser.
* Serving Flask app "pgadmin" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
啟動則只需要
$ source pgadmin4/bin/activate
(pgadmin4) $ pgadmin4
問題1 : 無法啟動pgAdmin
錯誤訊息
No module named '_sqlite3'
解決方法
首先先安裝所需要的套件
sudo apt update
sudo apt install libsqlite3-dev
接著要找到PYTHON的位置並且重新編譯
把下面的/path/to/python/source換成你的PYTHON的根目錄
cd /path/to/python/source
sudo ./configure --enable-optimizations
sudo make
sudo make altinstall
然後就可以正常啟動囉!
問題2:只能從127.0.0.1連線
使用下面指令找到套件位置
pip show pgadmin4 | grep Location
修改config
找到下面這行並將127.0.0.1改成0.0.0.0
DEFAULT_SERVER = '0.0.0.0'
問題3: 開啟防火牆5050端口
sudo systemctl start firewalld
sudo systemctl enable firewalld
sudo firewall-cmd --zone=public --add-port=5050/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --zone=public --list-ports
最後一行是用以檢查是否已打開,如果 5050/tcp 在列表中,則表示已成功打開。
接著把SELINUX的端口打開
sudo semanage port -a -t http_port_t -p tcp 5050
就可以成功連線啦!