WSL2 + PostgreSQL + pgAdmin4 环境搭建
本文最后更新于 277 天前,内容如有失效请评论区留言。

这里采用在 WSL2(Ubuntu 22.04) 上安装 PostgreSQL,再在 Windows 10 上通过 pgAdmin4 工具连接数据库的方法,便于日后学习和使用

安装 PostgreSQL

进入官网的下载页面,根据自己的系统选择对应的下载链接,传送门

image-20230316152853964

依次执行下面的命令,添加源地址和软件包密钥,然后更新源,安装 postgresql

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get -y install postgresql

image-20230316152033291

安装完成后查看版本信息

image-20230316154605629

安装 pgAdmin4

下载传送门

image-20230316152752362

image-20230316153005764

image-20230316153021387

安装完成后启动,设置 Master Password

image-20230316153147918

让 WSL2 的 PostgreSQL 接收来自 Windows 的连接请求

默认情况下, PostgreSQL 安装后仅监听 127.0.0.1。这意味着你无法从远程主机(包括 Windows 主机)访问数据库实例。这不是 Bug,这是一项安全功能。

执行下面命令来更改配置

sudo apt install gedit -y
sudo gedit /etc/postgresql/15/main/postgresql.conf
sudo service postgresql restart

注意到上面使用了 gedit这也是笔者刚刚才发现的,The latest WSL2 is so cool that it allows you to run GUI Linux applications! So instead of using a TUI editor like nano or vim, we will use Gedit!

image-20230316164407055

修改下 listen_addresses

image-20230316164205852

添加新用户到 PostgreSQL

默认情况下,安装好后只有一个超级用户 postgres 是可用的,建议创建新用户

$ sudo -u postgres psql
psql (15.2 (Ubuntu 15.2-1.pgdg22.04+1))
Type "help" for help.

postgres=# CREATE USER dev PASSWORD 'strongone' CREATEDB;
CREATE ROLE
postgres=# \q

现在我们可以指定我们新创建的用户 dev 并使用密码身份验证连接到 PostgreSQL

请注意,我明确使用了 -h 127.0.0.1 参数来强制进行 password authentication 而不是 peer authentication

$ psql -U dev -h 127.0.0.1 -d postgres
Password for user dev:
psql (15.2 (Ubuntu 15.2-1.pgdg22.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Type "help" for help.

postgres=>\q

image-20230316171137173

让 WSL2 上的 PostgreSQL 允许从远程主机进行身份验证

最简单的方法是向 pg_hba.conf 文件添加额外的行:

image-20230316171543233

此更改将为所有 IPv4 和 IPv6 连接应用 scram-sha-256 密码身份验证

image-20230316171654581

使用 pgAdmin 工具连接数据库

好,现在终于可用使用 pgAdmin 工具连接 WSL2 了,OhOhOh

image-20230316172215894

连接成功后看到如下界面,

image-20230316172944689

创建表

image-20230316174444139

插入数据

image-20230316174759719

执行 SQL 语句

image-20230316174916730

Reference

POSTGRESQL ON WSL2 FOR WINDOWS: INSTALL AND SETUP

Set up PostgreSQL on WSL2 and Access with pgAdmin on Windows

开始使用适用于 Linux 的 Windows 子系统上的数据库

psql官方文档

CREATE ROLE命令

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇