在Ubuntu上安装Postgres
创建postgres用户
$ sudo adduser postgres
$ sudo passwd postgres
按照公式的大致步骤安装最新版本的PostgreSQL。
https://www.postgresql.org/download/linux/ubuntu/
# 检查您计划下载的版本
$ sudo apt info postgresql
Package: postgresql
Version: 14+238 # 旧
# Create the file repository configuration:
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
$ sudo apt update
# 检查您计划下载的版本
$ sudo apt info postgresql
Package: postgresql
Version: 15+248.pgdg22.04+1 # 新
# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
$ sudo apt -y install postgresql
如果在旅途中遇到以下错误的情况
Setting up postgresql (15+248.pgdg22.04+1) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for libc-bin (2.35-0ubuntu3.1) ...
Scanning processes...
Scanning processor microcode...
Scanning linux images...
Running kernel seems to be up-to-date.
Failed to check for processor microcode upgrades.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
这样就处理了
$ sudo apt -y remove postgresql
$ apt purge needrestart
$ sudo apt -y install postgresql
寻找安装位置
$ sudo find / -name postgresql
/var/log/postgresql
/var/lib/postgresql
/usr/lib/postgresql
/usr/share/doc/postgresql
/usr/share/postgresql
/etc/postgresql
/run/postgresql
启动PostgreSQL
$ sudo systemctl enable postgresql
$ sudo systemctl start postgresql
数据库的初始设置
sudo su - postgres
vi .profile
export PGHOME=/usr/lib/postgresql/15
export PGDATA=/var/lib/postgresql/15/data
export PGLIB=$PGHOME/lib
export LD_LIBRARY_PATH=$PGHOME/lib
export PATH=$PATH:$PGHOME/bin
source ~/.profile
initdb -E UTF8 --no-locale
启动数据库
使用pg_ctl start命令