Ubuntu安装PostgreSQL

时间:2024-10-05 07:03:37

注:本文章的ubuntu的版本为:ubuntu-20.04.6-live-server-amd64。

Ubuntu(在线版)

更新软件源

sudo apt-get update

添加PostgreSQL官方数字签名

wget --quiet -O - https:///media/keys/ACCC4CF8.asc | sudo apt-key add -

将地址添加到系统的软件包源列表中

echo "deb /pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources./

更新软件源

sudo apt-get update

安装PostgreSQL

注:本文章安装PostgreSQL 16

sudo apt install postgresql-16

启动服务

systemctl start postgresql

查看PostgreSQL状态

systemctl status postgresql

看到activate即启动成功

Ubuntu(离线版)

进入下载链接选择PostgreSQL版本下载

/ftp/source/

注:本文章下载postgresql-16.

解压文件到指定目录

tar -xvf postgresql-16.3. -C /usr/local

进入解压后的目录

cd /usr/local/postgresql-16.3

配置安装目录

注:本文章安装的路径为/usr/local/pgsql

./configure --prefix=/usr/local/pgsql

可能报错:

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... no
checking for cc... no
configure: error: in `/usr/local/postgresql-16.3':
configure: error: no acceptable C compiler found in $PATH
See `' for more details
原因是在配置PostgreSQL的编译环境时遇到了问题,原因是系统中没有找到可接受的C编译器。报错指出configure脚本在$PATH环境变量指定的路径中没有找到gcc编译器。

建议输入以下命令,安装PostgreSQL需要的环境,包括gcc,g++,make等。

  1. sudo apt-get update # 更新软件源
  2. sudo apt-get install build-essential # 安装软件包,包括gcc、g++、make等

可能报错:

configure: error: ICU library not found
If you have ICU already installed, see for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-icu to disable ICU support.
原因是配置PostgreSQL编译选项时,系统试图启用ICU(International Components for Unicode)支持,但是没有找到ICU库。ICU库用于提供Unicode和国际化支持,如果PostgreSQL需要处理多语言文本或需要国际化特性,就需要安装ICU库。

  1. sudo apt-get update # 更新软件源
  2. sudo apt-get install libicu-dev # 安装ICU包

如果不需要ICU支持,或者只是想要快速安装PostgreSQL而不处理ICU的依赖问题,可以选择禁用ICU支持。

./configure --prefix=/usr/local/pgsql --without-icu

可能报错:

configure: error: readline library not found
If you have readline already installed, see for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
原因是配置PostgreSQL编译选项时,系统找不到readline库。readline库通常用于提供命令行编辑和历史记录功能,如果PostgreSQL需要提供命令行编辑和历史记录功能,就需要安装readline库。

  1. sudo apt-get update # 更新软件源
  2. sudo apt-get install libreadline-dev # 安装readline包

如果不需要readline支持,或者只是想要快速安装PostgreSQL而不处理readline的依赖问题,可以选择禁用readline支持。

./configure --prefix=/usr/local/pgsql --without-readline

可能报错:

configure: error: zlib library not found
If you have zlib already installed, see for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-zlib to disable zlib support.
原因是配置PostgreSQL编译选项时,系统找不到zlib库。zlib库是一个用于数据压缩的库,许多软件(包括PostgreSQL)都依赖它进行压缩和解压缩操作,如果PostgreSQL需要提供压缩和解压缩功能,就需要安装zlib库。

  1. sudo apt-get update # 更新软件源
  2. sudo apt-get install zlib1g-dev # 安装zlib包

如果你确定不需要zlib支持,或者只是想要快速安装PostgreSQL而不处理zlib的依赖问题,可以选择禁用zlib支持。

./configure --prefix=/usr/local/pgsql --without-zlib

注:如果需要同时禁用ICU、readline、zlib支持,可以输入的命令为:

./configure --prefix=/usr/local/pgsql --without-icu --without-readline --without-zlib

编译

make

编译安装

make install

创建数据文件夹

mkdir /usr/local/pgsql/data

创建postgres用户

  1. sudo groupadd postgres
  2. sudo useradd -g postgres -m -s /bin/bash postgres

设置数据目录的权限

sudo chown -R postgres:postgres /usr/local/pgsql/data

以postgres用户初始化数据库

sudo su - postgres -c "/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data"

启动服务

sudo su - postgres -c "/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start"

查看PostgreSQL状态

sudo su - postgres -c "/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data status"

看到server is running即启动成功

设置PostgreSQL密码

进入psql命令行

  1. sudo -u postgres psql # 本文章在线安装的postgresql
  2. sudo su - postgres -c "/usr/local/pgsql/bin/psql" # 本文章离线安装的postgresql

设置postgresql密码

alter user postgres with password 'new_password';  # 例如:alter user postgres with password '1';

测试postgresql

select datname from pg_database where has_database_privilege(datname, 'connect');

该SQL展示当前用户有权连接的数据库名称列表。

退出命令行

\q

设置远程连接

修改文件

  1. sudo vim /etc/postgresql/16/main/ # 在线安装
  2. sudo su postgres
  3. vim /usr/local/pgsql/data/ # 离线安装
  4. exit

将listen_addresses的值将其改为'*',放开注释。

修改pg_hba.conf文件

  1. sudo vim /etc/postgresql/16/main/pg_hba.conf # 在线安装
  2. sudo su postgres
  3. vim /usr/local/pgsql/data/pg_hba.conf # 离线安装
  4. exit

增加一行:host all all 0.0.0.0/0 md5(注意空格,最好跟其他内容对齐)

重启服务

  1. systemctl restart postgresql # 在线安装
  2. sudo su - postgres -c "/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data restart" # 离线安装