PostgreSQL基礎(chǔ)之安裝

1 PostgreSQL安裝

1.1 官網(wǎng)安裝

官網(wǎng):https://www.postgresql.org/

1.1.1 下載

c5d6b0b15fdf9d05f7d6b994081df1c6_697d7c337c2b437397c12a6e253c9ea6.png
8cfd3d7d92d875efed146615eeeeca54_3f1e7523bbe24c86adcd06c30828e622.png
e8475d193401dcef463d0b481f47fddc_4626121be6de44ecbc671079acd6a7e1.png
f581745a05da2301729cd89373655484_b6b02059464145aab05a201980c7345c.png

1.1.2 exe安裝

bf00af8d6e0ef201f4cbe1f6d62a5e38_b922b6eacdfe40e0b47a26e95909fbd1.png
9dac32a065df34bbbdd8032afc69c41c_491f0be6054f4d248c51e879d2dcb3a3.png
4a68a9480c7b222a2f5e0d7ad94d4ef0_36c8d3480c2244e8b231d0cfbb5d68f2.png
d970c9fe8f7f0cbb3166cb85500f5293_fd6e60670ca34a8f98f7447226549db8.png

數(shù)據(jù)庫相關(guān)數(shù)據(jù)默認(rèn)存放在這里


713aabe6feb4d20b4dacf5f3c619d139_c10875d5ef2b4f61a9efac61797dd919.png

設(shè)置密碼,賬號(hào)默認(rèn)為postgres


8c39b9c8590bb65fa89fc87a3f466d89_bae00791648945bb94bceb899d6116fa.png

數(shù)據(jù)庫服務(wù)監(jiān)聽端口號(hào),默認(rèn)端口號(hào)即可


73bf9f01675af3a31989704f10c48599_ffdd970c167043a48beb920b8acf0bae.png

系統(tǒng)語言/地區(qū), 建議選擇默認(rèn)


948fbfacc7b9959e36009fd6b1c5f24a_5ce1c03c7ebb4cd9b4664437b8125aaf.png

1.1.3 zip安裝

下載解壓:

解壓到 C:\pgsql
# 解壓后結(jié)構(gòu):
C:\pgsql\
├── bin\          # 可執(zhí)行文件
├── lib\          # 庫文件
├── share\        # 共享文件、擴(kuò)展
├── symbols\      # 調(diào)試符號(hào)(可刪除)
└── pgAdmin 4\    # 圖形工具(部分版本)

初始化數(shù)據(jù)庫集群
這是最關(guān)鍵的一步,創(chuàng)建數(shù)據(jù)目錄和系統(tǒng)表:

# 創(chuàng)建數(shù)據(jù)目錄
mkdir C:\pgsql\data

# 初始化(指定編碼、用戶名、密碼)
# -D: 數(shù)據(jù)目錄 -E: 編碼 UTF8 -U: 超級(jí)用戶名(默認(rèn) postgres) -W: 交互式輸入密碼(推薦)
C:\pgsql\bin\initdb.exe -D C:\pgsql\data -E UTF8 -U postgres -W 

輸出示例:
The files belonging to this database system will be owned by user "你的用戶名".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
...
Success. You can now start the database server using:

    "C:\pgsql\bin\pg_ctl" -D "C:\pgsql\data" -l logfile start

啟動(dòng) PostgreSQL

方式一:前臺(tái)啟動(dòng)(調(diào)試/臨時(shí))

# 前臺(tái)運(yùn)行,Ctrl+C 停止
C:\pgsql\bin\postgres.exe -D C:\pgsql\data

方式二:后臺(tái)啟動(dòng)(推薦)
# 后臺(tái)服務(wù)方式啟動(dòng)
C:\pgsql\bin\pg_ctl.exe -D C:\pgsql\data start -l C:\pgsql\log\pg.log
這里得log 文件夾要存在,不然第一次可能報(bào):could not open log file

如果啟動(dòng)時(shí)不想加 -D 指定數(shù)據(jù)庫,那么添加環(huán)境變量:PGDATA來指定數(shù)據(jù)目錄

# 停止
C:\pgsql\bin\pg_ctl.exe -D C:\pgsql\data stop

# 重啟
C:\pgsql\bin\pg_ctl.exe -D C:\pgsql\data restart

方式三:注冊為 Windows 服務(wù)(生產(chǎn)環(huán)境)
# 以管理員身份運(yùn)行 PowerShell
C:\pgsql\bin\pg_ctl.exe register -N "PostgreSQL" -D "C:\pgsql\data" -S auto

# 啟動(dòng)服務(wù)
net start PostgreSQL

# 停止服務(wù)
net stop PostgreSQL

# 刪除服務(wù)
C:\pgsql\bin\pg_ctl.exe unregister -N "PostgreSQL"

連接測試

# 使用 psql 連接
C:\pgsql\bin\psql.exe -U postgres -d postgres

# 或者指定主機(jī)(本地)
C:\pgsql\bin\psql.exe -h localhost -U postgres -d postgres

進(jìn)入 psql 后:
-- 查看版本
SELECT version();

-- 修改密碼 還需要改data目錄下的  pg_hba.conf 文件把里面的 trust 改為 scram-sha-256 這樣登錄就需要密碼而非trust
\password postgres



-- 列出數(shù)據(jù)庫
\l

-- 創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE mydb;

-- 退出
\q

1.2 Scoop安裝

點(diǎn)擊了解Win環(huán)境下包管理工具

1.2.1 查詢PostgreSQL

C:\Users\test>scoop search postgresql
Results from local buckets...

Name         Version Source   Binaries
----         ------- ------   --------
postgresql   18.2    main
postgresql10 10.23   versions
postgresql11 11.22   versions
postgresql12 12.22   versions
postgresql13 13.23   versions
postgresql14 14.21   versions
postgresql15 15.16   versions
postgresql16 16.12   versions
postgresql17 17.8    versions

1.2.2 安裝

C:\Users\test>scoop install main/postgresql

Installing 'postgresql' (18.3) [64bit] from 'main' bucket
proxy: https://get.enterprisedb.com/postgresql/postgresql-18.3-1-windows-x64-binaries.zip
Starting download with aria2 ...
Download: Download Results:
Download: gid   |stat|avg speed  |path/URI
Download: ======+====+===========+=======================================================
Download: 55d75c|OK  |    24MiB/s|D:/Scoop/cache/postgresql#18.3#f0c3152.zip
Download: Status Legend:
Download: (OK):download completed.
Checking hash of postgresql-18.3-1-windows-x64-binaries.zip ... ok.
Extracting postgresql-18.3-1-windows-x64-binaries.zip ... done.
Linking D:\Scoop\apps\postgresql\current => D:\Scoop\apps\postgresql\18.3
Creating shortcut for pgAdmin 4 (pgAdmin4.exe)
Adding D:\Scoop\apps\postgresql\current\bin to your path.
Persisting data
Running post_install script...屬于此數(shù)據(jù)庫系統(tǒng)的文件宿主為用戶 "test".
此用戶也必須為服務(wù)器進(jìn)程的宿主.

數(shù)據(jù)庫集簇將以區(qū)域環(huán)境設(shè)置 "C" 進(jìn)行初始化.
默認(rèn)的文本搜索配置將被設(shè)為 "english".

允許生成數(shù)據(jù)頁校驗(yàn)和.

正在修復(fù)已存在目錄 D:/Scoop/apps/postgresql/current/data 的權(quán)限 ... 成功
正在創(chuàng)建子目錄 ... 成功
正在選擇動(dòng)態(tài)共享內(nèi)存實(shí)現(xiàn) ... windows
正在選擇默認(rèn)最大連接數(shù) (max_connections) ... 100
正在選擇默認(rèn)共享緩沖區(qū) (shared_buffers) ... 128MB
正在選擇默認(rèn)時(shí)區(qū) ... Asia/Shanghai
正在創(chuàng)建配置文件 ... 成功
正在運(yùn)行啟動(dòng)腳本 ... 成功
正在執(zhí)行啟動(dòng)腳本后續(xù)初始化 ... 成功
正在同步數(shù)據(jù)到磁盤 ... 成功

initdb: 警告: 為本地連接啟用 "trust" 身份驗(yàn)證
initdb: 提示: 你可以通過編輯 pg_hba.conf 或下次運(yùn)行 initdb 時(shí)使用 -A 或者 --auth-local 和 --auth-host 選項(xiàng)進(jìn)行更改.

成功。你現(xiàn)在可以用下面的命令開啟數(shù)據(jù)庫服務(wù)器:

    ^"D^:^\Scoop^\apps^\postgresql^\current^\bin^\pg^_ctl^" -D ^"D^:^\Scoop^\apps^\postgresql^\current^\data^" -l logfile start

done.
'postgresql' (18.3) was installed successfully!
Notes
-----
Run 'pg_ctl start' to start the database and 'pg_ctl stop' to stop it.
Register it as a service by running 'pg_ctl register -N PostgreSQL' from an elevated shell.
Default superuser login: postgres, password: <blank>

1.2.3 修改密碼

啟動(dòng)

pg_ctl start

等待服務(wù)器進(jìn)程啟動(dòng) ....2026-02-28 11:15:24.197 CST [10924] LOG:  starting PostgreSQL 18.3 on x86_64-windows, compiled by msvc-19.44.35222, 64-bit
2026-02-28 11:15:24.201 CST [10924] LOG:  listening on IPv6 address "::1", port 5432
2026-02-28 11:15:24.201 CST [10924] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2026-02-28 11:15:24.522 CST [29296] LOG:  database system was shut down at 2026-02-28 11:14:12 CST
2026-02-28 11:15:24.567 CST [10924] LOG:  database system is ready to accept connections
 完成
服務(wù)器進(jìn)程已經(jīng)啟動(dòng)

登錄修改密碼

登錄
psql -U postgres -d postgres

修改密碼
\password postgres
Enter new password for user "postgres":
再輸入一遍:

修改完密碼記得把數(shù)據(jù)目錄data下的 pg_hba.conf改下,把里面的 trust 改成 scram-sha-256,trust是不需要密碼的

1.3 使用pgAdmin

f7f0ef0d09edc8d444f16d8d6707f950_129c081e63b849278fc3115b56a9838e.png

打開如下:


0aafa02d5431af7f49ebd50ecdc902cf_fef002ea8bbf43219300ec4a19d57a63.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容