1. 准备工作
如需安装系统支持版本:
$ sudo apt update
$ sudo apt-get install postgresql postgresql-contrib libpq-dev
$ sudo systemctl status postgresql
gpg 安装方法:
$ curl -L https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /usr/share/keyrings/postgresql-archive-keyring.gpg >/dev/null
$ echo "deb [arch=amd64 signed-by=/usr/share/keyrings/postgresql-archive-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list
$ sudo apt update
$ sudo apt install postgresql libpq-dev
$ sudo systemctl start postgresql
如需安装 PostgreSQL 12 版本:
$ 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 update
$ sudo apt -y install postgresql-12 libpq-dev
如需删除 Postgres Key:
$ sudo apt-key list
$ sudo apt-key del
$ sudo apt update
2. 连接 PostgreSQL
$ sudo su - postgres
$ psql
$ postgres=# \conninfo
返回:
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
3. PostgreSQL 安全
PostgreSQL 安装程序会在您的系统上创建一个用户 "postgres", 该用户未设置任何密码。
首先,通过运行以下命令为 "postgres" 用户帐户创建密码。
$ sudo passwd postgres
接下来,切换到 "postgres" 并为 PostgreSQL 管理数据库用户创建一个安全的强密码,如下所示:
$ su - postgres
$ psql -c "ALTER USER postgres WITH PASSWORD 'YOURNEWPASSWORD';"
$ exit
最后,重新启动服务,使设置生效。
$ sudo systemctl restart postgresql