Skip to content

PostgreSQL安装教程 ​

源码安装 ​

安装教程

https://www.postgresql.org/docs/17/installation.html

中文版(15.7)http://www.postgres.cn/docs/current/installation.html

1、下载源码

https://www.postgresql.org/ftp/source/

以版本17.2为例

bash
wget https://ftp.postgresql.org/pub/source/v17.2/postgresql-17.2.tar.bz2

tar xf postgresql-17.2.tar.bz2

2、安装

bash
./configure --prefix=/usr/local/pgsql --enable-debug --enable-cassert --enable-depend CFLAGS=-O0

make && make install

3、添加到环境变量

bash
# vim ~/.bash_profile
# pgsql
PG_HOME='/usr/local/pgsql/'
export PGDATA='/usr/local/postgres-data'
export PATH="$PG_HOME/bin":$PATH
export LD_LIBRARY_PATH='$PG_HOME/lib':$LD_LIBRARY_PATH

4、初始化数据库

bash
mkdir -p /usr/local/pgsql/data

initdb -D /usr/local/pgsql/data

pg_ctl -D /usr/local/pgsql/data -l logfile start

createdb test

psql test

create table tb_test(id int, name varchar(20));

select * from tb_test;

如果设置了环境变量PGDATA ,可以不指定-D参数

遇到的问题 ​

1、configure: error: ICU library not found

shell
$ ./configure
checking for icu-uc icu-i18n... no
configure: error: ICU library not found
If you have ICU already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-icu to disable ICU support.

解决办法

bash
$ icu-config --version
-bash: icu-config: 未找到命令

yum install libicu-devel -y

$ icu-config --version
50.2

2、configure: error: readline library not found

bash
$ ./configure
checking for library containing readline... no
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.

解决办法

bash
$ yum install -y readline-devel

3、initdb: symbol lookup error: initdb: undefined symbol: pqsignal

问题

bash
$ initdb -D /usr/local/pgsql/data

initdb: symbol lookup error: initdb: undefined symbol: pqsignal

解决办法

设置环境

bash
export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH

3、postgresql gdb which has no line number information

bash
make uninstall && make clean && make && make install

linux 安装gcc---GMP 4.2+, MPFR 3.1.0+ and MPC 0.8.0+ https://www.cnblogs.com/zhiminyu/p/18267733

扩展安装 ​

第一步:编译c扩展

shell
cd contrib/extension_name
make && make install

该步骤会执行以下操作

(1) 编译C扩展 (2) 然后将extension_name.control 和 extension_name--version.sql 拷贝到SHAREDIR/extension (3) 将共享库拷贝到 PKGLIBDIR

例如: 安装pageinspect

shell
cd contrib/extension_name
make && make install

安装完成后目录如下

shell
lib/
    pageinspect.dylib
share/
    extension/
        pageinspect.control
        pageinspect--1.0--1.1.sql
        pageinspect--1.1--1.2.sql
        ...

第二步:创建扩展

sql
CREATE EXTENSION pageinspect;

使用扩展

sql
drop table if exists t1;
create table t1(id int, age int);

begin;
insert into t1 (id, age) values (1, 1);
select txid_current();

\x
select * from heap_page_items(get_raw_page('t1', 0));
end;

vscode psql执行sql脚本 ​

json
{
    "code-runner.executorMap": {
        "sql": "cd $dir && psql --dbname=db_test --echo-all --set VERBOSITY=verbose --file $fileName"
    }
}