ubuntu16.04下安裝postgresql 10.3
環境及資源
Linux版本:ubuntu16.04
PostgresSQL版本:postgresql-10.3
PostgreSQL官網下載地址:https://www.postgresql.org/ftp/source/v10.3/
下載postgresql-10.3.tar.gz
正文
安裝路徑為/usr/local/pgsql/
1、解壓
gunzip postgresql-10.3.tar.gz
tar xf postgresql-10.3.tar
2、安裝
在 /usr/local/pgsql/postgresql-10.3 目錄下
執行 ./configure 檢視當前環境是否支援安裝
發現需要安裝依賴
checking for library containing readline... no
configure: error: readline library not found
解決:apt-get install libreadline-dev
checking for inflate in -lz... no
configure: error: zlib library not found
解決:apt-get install zlib1g
apt-get install zlib1g.dev
執行 make
All of PostgreSQL successfully made. Ready to install.
執行 make install 安裝postgresql
PostgreSQL installation complete.
執行 adduser postgres 新增postgres 使用者
執行 mkdir data
chown postgres /usr/local/pgsql/data
執行 /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &
3、啟動資料庫
執行 su - postgres 切換使用者
執行 usr/local/pgsql/pg_ctl start -D /usr/local/pgsql/data 啟動資料庫
附: 重啟資料庫: usr/local/pgsql/pg_ctl restart -D /usr/local/pgsql/data
關閉資料庫: usr/local/pgsql/pg_ctl stop -D /usr/local/pgsql/data
4、建立資料庫及使用者
執行 ./createdb dbname 建立資料庫
執行 ./createuser -P dbuser 建立dbuser使用者
5、訪問資料庫
執行 ./psql
退出 ctrl + d
訪問資料庫後也可以用sql命令來新建使用者
create user dbuser with password 'xxx';
6、遠端訪問資料庫
需設定兩個配置檔案
1)修改配置檔案postgresql.conf
vim /usr/local/pgsql/data/postgresql.conf
修改監聽地址:
#listen_addresses='localhost'
#將上面這行改成如下
listen_addresses='*'
listen_address 和 port 預設是被註釋的,均要取消註釋。
2)修改配置檔案/pgsql/data/pg_hba.conf:
vim /usr/local/pgsql/data/pg_hba.conf
新增一條IP授權記錄,可以對一個網段授權
# IPv4 myhost connections:
host all all 0.0.0.0/0 trust
修改完成後重啟資料庫即可
附:我是租用的阿里雲伺服器,在阿里雲伺服器上也需要設定一下出入埠(postgresql預設埠為5432)
7、使用windows下圖形化工具navicat訪問資料庫
按照之前新增的使用者設定一下就ok啦。