初識PostgreSQL

dl_lang發表於2021-04-12

初識PostGresql

公司的一個專案可能需要使用PG,就學習一下,給自己留一個筆記吧:

1、新增使用者

[root@localhost bin]# cat /etc/redhat-release

CentOS Linux release 7.4.1708 (Core)

#groupadd postgres

#useradd -g postgres postgres

#passwd postgres


2、安裝PG 12.6

在官網下載選單,按作業系統選擇:

Linux --> Redhat/CentOS -->Version 12 --> PlatForm-->Version 7 --> X86_64

# Install the repository RPM:

sudo yum install -y


# Install PostgreSQL:

sudo yum install -y postgresql12-server


# Optionally initialize the database and enable automatic start:

sudo /usr/pgsql-12/bin/postgresql-12-setup initdb   ##初始化

sudo systemctl enable postgresql-12 

sudo systemctl start postgresql-12


3、設定

[postgres@localhost ~]$ cd /var/lib/pgsql/12/data/   初始的資料檔案目錄

[postgres@localhost bin]$ pwd

/usr/pgsql-12/bin    初始二進位制命令和配置檔案目錄

##檢視執行狀態

[postgres@localhost bin]$ ./pg_ctl status -D /var/lib/pgsql/12/data

pg_ctl: 正在執行伺服器程式(PID: 1739)

/usr/pgsql-12/bin/postgres "-D" "/var/lib/pgsql/12/data"

##停止資料庫

[postgres@localhost bin]$ ./pg_ctl stop -D /var/lib/pgsql/12/data

等待伺服器程式關閉 .... 完成

伺服器程式已經關閉

##啟動資料庫

[postgres@localhost bin]$ ./pg_ctl start -D /var/lib/pgsql/12/data

等待伺服器程式啟動 ....2021-04-12 16:49:59.955 CST [2024] 日誌:  正在啟動 PostgreSQL 12.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit

2021-04-12 16:49:59.957 CST [2024] 日誌:  正在監聽IPv4地址"0.0.0.0",埠 5432

2021-04-12 16:49:59.957 CST [2024] 日誌:  正在監聽IPv6地址"::",埠 5432

2021-04-12 16:49:59.960 CST [2024] 日誌:  在Unix套接字 "/var/run/postgresql/.s.PGSQL.5432"上偵聽

2021-04-12 16:49:59.965 CST [2024] 日誌:  在Unix套接字 "/tmp/.s.PGSQL.5432"上偵聽

2021-04-12 16:49:59.971 CST [2024] 日誌:  日誌輸出重定向到日誌收集程式

2021-04-12 16:49:59.971 CST [2024] 提示:  後續的日誌輸出將出現在目錄 "log"中.

 完成

伺服器程式已經啟動

##配置遠端訪問

[postgres@localhost data]$ pwd

/var/lib/pgsql/12/data

[postgres@localhost data]$ vi pg_hba.conf

# IPv4 local connections:

host    all             all             127.0.0.1/32            ident

host    all             all             0.0.0.0/0            password


[postgres@localhost data]$ vi postgresql.conf

listen_addresses = '*'          # what IP address(es) to listen on;

#listen_addresses = 'localhost'         # what IP address(es) to listen on;


重新啟動資料


[postgres@localhost bin]$ ./psql

psql (12.6)

輸入 "help" 來獲取幫助資訊.


postgres-# \l

                                     資料庫列表

   名稱    |  擁有者  | 字元編碼 |  校對規則   |    Ctype    |       存取許可權


-----------+----------+----------+-------------+-------------+------------------

-----

 mydb      | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |

 postgres  | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 |

 template0 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres

    +

           |          |          |             |             | postgres=CTc/post

gres

 template1 | postgres | UTF8     | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres

    +

           |          |          |             |             | postgres=CTc/post

gres

(4 行記錄)


postgres-# \c postgres

您現在已經連線到資料庫 "postgres",使用者 "postgres".

postgres=# ALTER USER postgres WITH PASSWORD 'welcome1';

ALTER ROLE

postgres=# \q

[postgres@localhost bin]$ ./psql -U postgres -d postgres -h 192.168.56.10 -p 5432

使用者 postgres 的口令:

psql (12.6)

輸入 "help" 來獲取幫助資訊.


postgres=# \q


Cool!! 比Oracle 安裝簡單太多了。。。。。。










來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6126/viewspace-2767638/,如需轉載,請註明出處,否則將追究法律責任。