Linux中安裝PostgreSQL-10.1

環境說明html

    下載符合本身操做系統的版本便可( postgresql-10.1.tar.gzlinux

    我下載的是Windows 64 bit(zip archive):dbeaverce614.zip(dbeaver-ce-6.1.4-x86_64-setup)sql

    我下載的是:PostgreSQL JDBC 4.2 Driver, 42.1.4數據庫

 

安裝部署bootstrap

將下載好的源碼包上傳到服務器/tmp/目錄下centos

1 [root@VM_0_16_centos tmp]# ls | grep postgresql
2 postgresql-10.1.tar.gz

 

安裝路徑爲:/usr/local/pgsql服務器

1.解壓socket

1 [root@VM_0_16_centos tmp]# tar -zxf postgresql-10.1.tar.gz -C /usr/local/
2 [root@VM_0_16_centos tmp]# cd /usr/local/postgresql-10.1/

 

2.編譯安裝async

依次執行以下操做:post

[root@VM_0_16_centos postgresql-10.1]# ./configure
checking for getpwuid_r... yes
checking for gethostbyname_r... yes
checking whether strerror_r returns int... no
checking for main in -lm... yes
checking for library containing setproctitle... no
checking for library containing dlopen... -ldl
checking for library containing socket... none required
checking for library containing shl_load... no
checking for library containing getopt_long... none required
checking for library containing crypt... -lcrypt
checking for library containing shm_open... -lrt
checking for library containing shm_unlink... none required
checking for library containing clock_gettime... none required
checking for library containing fdatasync... none required
checking for library containing sched_yield... none required
checking for library containing gethostbyname_r... none required
checking for library containing shmget... none required
checking for library containing readline... no
configure: error: readline library not found If you have readline already installed, see config.log for details on the failure. It is possible the compiler isn't looking in the proper directory. Use --without-readline to disable readline support.

如上報錯參考:https://www.linuxidc.com/Linux/2012-02/53982.htm

報錯1:configure: error: no acceptable C compiler found in $PATH:

解決:yum install gcc

 

報錯2:configure: error: no acceptable C compiler found in $PATH:

根據提示大概是readline包未安裝

解決:yum install readline-devel

 

安裝readline-devel包

1 [root@VM_0_16_centos postgresql-10.1]# yum -y install readline-devel

Loaded plugins: fastestmirror, langpacks
Repository epel is listed more than once in the configuration
Loading mirror speeds from cached hostfile
epel | 5.3 kB 00:00:00
extras | 3.4 kB 00:00:00
os | 3.6 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): epel/7/x86_64/updateinfo | 997 kB 00:00:00
(2/2): epel/7/x86_64/primary_db | 6.8 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package readline-devel.x86_64 0:6.2-10.el7 will be installed
--> Processing Dependency: ncurses-devel for package: readline-devel-6.2-10.el7.x86_64
--> Running transaction check
---> Package ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================================
Installing:
readline-devel x86_64 6.2-10.el7 os 138 k
Installing for dependencies:
ncurses-devel x86_64 5.9-14.20130511.el7_4 os 712 k

Transaction Summary
========================================================================================================================================================================
Install 1 Package (+1 Dependent package)

Total download size: 851 k
Installed size: 2.4 M
Downloading packages:
(1/2): readline-devel-6.2-10.el7.x86_64.rpm | 138 kB 00:00:00
(2/2): ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm | 712 kB 00:00:00
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 5.3 MB/s | 851 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ncurses-devel-5.9-14.20130511.el7_4.x86_64 1/2
Installing : readline-devel-6.2-10.el7.x86_64 2/2
Verifying : readline-devel-6.2-10.el7.x86_64 1/2
Verifying : ncurses-devel-5.9-14.20130511.el7_4.x86_64 2/2

Installed:
readline-devel.x86_64 0:6.2-10.el7

Dependency Installed:
ncurses-devel.x86_64 0:5.9-14.20130511.el7_4

Complete!

安裝完後再次運行./configure便可

[root@VM_0_16_centos postgresql-10.1]# ./configure

 

編譯安裝

1 [root@VM_0_16_centos postgresql-10.1]# make
2 [root@VM_0_16_centos postgresql-10.1]# su
3 [root@VM_0_16_centos postgresql-10.1]# make install
 1 [root@VM_0_16_centos postgresql-10.1]# adduser postgres
 2 [root@VM_0_16_centos postgresql-10.1]# mkdir /usr/local/pgsql/data
 3 [root@VM_0_16_centos postgresql-10.1]# chown postgres /usr/local/pgsql/data
 4 [root@VM_0_16_centos local]# su - postgres
 5 Last login: Tue Aug 20 09:49:49 CST 2019 on pts/0
 6 [postgres@VM_0_16_centos ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
 7 The files belonging to this database system will be owned by user "postgres".
 8 This user must also own the server process.
 9 
10 The database cluster will be initialized with locale "C".
11 The default database encoding has accordingly been set to "SQL_ASCII".
12 The default text search configuration will be set to "english".
13 
14 Data page checksums are disabled.
15 
16 fixing permissions on existing directory /usr/local/pgsql/data ... ok
17 creating subdirectories ... ok
18 selecting default max_connections ... 100
19 selecting default shared_buffers ... 128MB
20 selecting dynamic shared memory implementation ... posix
21 creating configuration files ... ok
22 running bootstrap script ... ok
23 performing post-bootstrap initialization ... ok
24 syncing data to disk ... ok
25 
26 WARNING: enabling "trust" authentication for local connections
27 You can change this by editing pg_hba.conf or using the option -A, or
28 --auth-local and --auth-host, the next time you run initdb.
29 
30 Success. You can now start the database server using:
31 
32     /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start
33 [postgres@VM_0_16_centos ~]$ /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/ > logfile 2>&1 & 34 [1] 12283
35 [postgres@VM_0_16_centos ~]$ /usr/local/pgsql/bin/createdb test
36 [postgres@VM_0_16_centos ~]$ /usr/local/pgsql/bin/psql test
37 psql (10.1)
38 Type "help" for help.
39 
40 test=#

 

3.啓動或重啓服務

確保切換到/usr/local/pgsql/bin/目錄下,並切換爲postgres用戶

1 1 [postgres@VM_0_16_centos ~]$ cd /usr/local/pgsql/bin/
2 2 [postgres@VM_0_16_centos bin]$ su - postgres  #輸入密碼,但安裝時不知道密碼,若是已在該用戶下可不切換,默認第一次進來不輸入密碼

 

啓動服務

3 [postgres@VM_0_16_centos bin]$ ./pg_ctl start -D /usr/local/pgsql/data/
4 pg_ctl: another server might be running; trying to start server anyway
5 waiting for server to start....2019-08-20 10:18:45.034 CST [16027] FATAL:  lock file "postmaster.pid" already exists
6 2019-08-20 10:18:45.034 CST [16027] HINT:  Is another postmaster (PID 12283) running in data directory "/usr/local/pgsql/data"?
7  stopped waiting
8 pg_ctl: could not start server
9 Examine the log output.
啓動不了,是因爲上面在後臺運行的語句,以及產生了pid文件(/usr/local/pgsql/data)

[postgres@VM_0_16_centos bin]$ pkill 12283
[postgres@VM_0_16_centos bin]$ ./pg_ctl start -D /usr/local/pgsql/data/
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2019-08-20 10:19:23.881 CST [16139] FATAL: lock file "postmaster.pid" already exists
2019-08-20 10:19:23.881 CST [16139] HINT: Is another postmaster (PID 12283) running in data directory "/usr/local/pgsql/data"?
stopped waiting
pg_ctl: could not start server
Examine the log output.

將pid文件刪除便可

[postgres@VM_0_16_centos bin]$ rm -rf /usr/local/pgsql/data/postmaster.pid

再次運行啓動命令,以下圖即爲成功

 

至此PostgreSQL安裝完成

 

重啓服務

1 [postgres@VM_0_16_centos bin]$ ./pg_ctl restart -D /usr/local/pgsql/data/
  

 

 4.新建數據庫和能夠登陸數據庫的用戶密碼

確保切換到/usr/local/pgsql/bin/目錄下

建立數據庫:

1 [postgres@VM_0_16_centos bin]$ ./createdb thydb

 

建立用戶(用戶名:thy,密碼:TanHuiyuan)兩種方式:

第一種:CREATE USERCREATE ROLECREATE USER是CREATE ROLE的一個別名。 惟一的區別是CREATE USER命令缺省是LOGIN, 而CREATE ROLE命令缺省是NOLOGIN

進入默認數據庫postgres:

1 [postgres@VM_0_16_centos bin]$ ./psql
2 psql (10.1)
3 Type "help" for help.

 

執行(區分大小寫):

1 postgres=# CREATE USER thy WITH PASSWORD 'TanHuiyuan';
2 CREATE ROLE

 

建立成功以下:

 

第二種:pg封裝的命令方式:

若是已經存在改用戶了則不能重複建立

1 [postgres@VM_0_16_centos bin]$ ./createuser -P thy
  

 

5.訪問數據庫

確保切換到/usr/local/pgsql/bin/目錄下

以默認用戶名訪問默認數據庫(默認的用戶名和數據庫名都是postgres),進入PG交互終端

1 [postgres@VM_0_16_centos bin]$ ./psql
  

 

 以名爲thy角色登陸名爲thydb的數據庫:

1 [postgres@VM_0_16_centos bin]$ ./psql thydb -U thy
  

 

能夠看出,當psql終端的提示符爲=#時,表示當前登陸的是超級用戶,而當提示符爲=>時則爲普通用戶

 

6.遠程訪問數據庫設置

遠程訪問數據庫的認證方式主要有不少方式,這裏設置基於TCP/IP鏈接的trust認證方式

須要設置兩個配置文件:

1)修改配置文件postgresql.conf

1 [root@VM_0_16_centos ~]# vi /usr/local/pgsql/data/postgresql.conf

 

修改監聽地址:

1  #listen_addresses = 'localhost'
2 改成
3  listen_addresses = '*'
 listen_addresses = '*'

 

 

2)修改配置文件/pgsql/data/pg_hba.conf

1 [root@VM_0_16_centos ~]# vi /usr/local/pgsql/data/pg_hba.conf

 添加一條IP受權記錄(如182.254.184.102),能夠對一個網段受權

1  # IPv4 myhost connections:
2     host    all             all             182.254.184.0/24        trust

 

也能夠設置全部網段ip訪問

1 # IPv4 remove connections:

    host all all 0.0.0.0/0 trust

 

 

配置以後需重啓數據庫纔會生效:

需在postgres用戶下執行:

1 [postgres@VM_0_16_centos ~]$ /usr/local/pgsql/bin/pg_ctl restart -D /usr/local/pgsql/data/

 

7.Windows圖形化界面DBeaver客戶端訪問PG

Dbeaver須要指定jdbc驅動,可到https://jdbc.postgresql.org/download.html#current下載

因爲前面設置的是基於TCP/IP的trust認證,因此這裏也只要設置以下參數就可:

將下載好的安裝包解壓到相應的盤符

安裝步驟參考:https://jingyan.baidu.com/article/0aa223756a2eed88cd0d644a.html

Host:搭建PG的主機ip地址,這裏我把本身的主機IP碼掉啦

Port:默認爲5432

Database:就是咱們前面創建的數據庫thydb

User:前面建立的用戶名thy

Password:用戶名對應的密碼TanHuiyuan

 本次使用默認數據庫

驅動配置參考:https://jingyan.baidu.com/article/7c6fb428ab34e1c0642c9096.html

成功鏈接

 

 

參考博客:http://www.javashuo.com/article/p-xvuicxho-bs.html

相關文章
相關標籤/搜索