Skip to main content

PostgreSQL

在 Ubuntu 20.04 上安装 PostgreSQL

安装

sudo apt install postgresql postgresql-contrib

如果是正常的 Ubuntu,你可以使用下面的命令来启动数据库:

sudo systemctl start postgresql.service

如果在不支持 Systemd 的 WSL 上,就没法使用上面这个命令。参见:System has not been booted with systemd as init system (PID 1). Can't operate

改用下面的命令来启动数据库:

sudo service postgresql start

进入数据库:

sudo -u postgres psql

参见:Get started with databases on Windows Subsystem for Linux

初始化账号

创建 Linux 用户:

sudo adduser pg1

设置密码。

切换到主账号:

sudo -iu postgres

创建新角色(role),名字同上(pg1):

createuser --interactive

创建数据库:

createdb pg1

改变用户密码

登录:

sudo -u postgres psql

修改密码:

\password pg1

要修改密码才能连接成功,好奇怪的问题。