linux postgresql默认安装目录,postgresql - 三种安装方式(示例代码)

本文介绍了PostgreSQL数据库的三种安装方式:RPM包安装、yum安装和源码包安装,详细阐述了每种方式的步骤,包括初始化数据库、设置用户密码等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近接触了postgresql的安装,和大家分享一下。

一、简 介

PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统。有些特性甚至连商业数据库都不具备。这个起源于伯克利(BSD)的数据库研究计划目前已经衍生成一项国际开发项目,并且有非常广泛的用户。

二、三种安装过程

A. RPM包安装

1. 检查PostgreSQL 是否已经安装

rpm -qa|grep postgres

若已经安装,则使用rpm -e 命令卸载。

2. 下载RPM包

3. 安装PostgreSQL,注意安装顺序

# rpm -ivh postgresql92-libs-9.2.4-1PGDG.rhel6.i686.rpm

# rpm -ivh postgresql92-9.2.4-1PGDG.rhel6.i686.rpm

# rpm -ivh postgresql92-server-9.2.4-1PGDG.rhel6.i686.rpm

# rpm -ivh postgresql92-contrib-9.2.4-1PGDG.rhel6.i686.rpm

4. 初始化PostgreSQL库

PostgreSQL 服务初次启动的时候会提示初始化。

a05df11d34a5ee46744c13b9f622d427.png

# service postgresql-9.2 initdb

23c4d347413f0e134b248f5d6e45724a.png

5. 启动服务

# service postgresql-9.2 start

bafd8b47ee8d0dc5abafb4de3b3f17ea.png

6. 把PostgreSQL 服务加入到启动列表

# chkconfig postgresql-9.2on

# chkconfig --list|grep postgres

7. 修改PostgreSQL数据库用户postgres的密码(注意不是linux系统帐号)

PostgreSQL数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为’postgres’。

# su - postgres

$ psql

# ALTERUSER postgres WITH PASSWORD ‘postgres‘;

# select*from pg_shadow ;

bfe57347d977eba0ae9b53bdfe1ac975.png

B. yum 安装

1. 将刚才安装的PostgreSQL 卸载

# /etc/init.d/postgresql-9.2 stop  //停止PostgreSQL服务

//查看已安装的包

# rpm -qa|grep postgres

//卸载

# rpm -e postgresql92-server-9.2.4-1PGDG.rhel6.i686

# rpm -e postgresql92-contrib-9.2.4-1PGDG.rhel6.i686

# rpm -e postgresql92-9.2.4-1PGDG.rhel6.i686

# rpm -e postgresql92-libs-9.2.4-1PGDG.rhel6.i686

2. yum 安装

如果是默认yum 安装的话,会安装较低版本的PostgreSQL 8.4,这不符合我们的要求。

d8ca1a276fb1bebb946b49c1c0727b38.png

我们使用PostgreSQL Yum Repository 来安装最新版本的PostgreSQL。

2.1 安装PostgreSQL yum repository

# rpm -i http://yum.postgresql.org/9.2/RedHat/rhel-6-x86_64/pgdg-redhat92-9.2-7.noarch.rpm

2.2 安装新版本PostgreSQL

# yum install postgresql92-server postgresql92-contrib

2.3 查看安装

6b739ff0b5169889860f303af2309184.png

3. 初始化并启动数据库

4e2ea91e54044b0222827fb8175365ff.png

配置文件:/var/lib/pgsql/data/pg_hba.conf

4. 测试

67da594d78a0a331dacd790e58853779.png

其他步骤如A方式。

C. 源码包安装

1、下载Postgresql源代码

2、解压该文件

tar xjvf postgresql-9.0.3.tar.bz2

3、进入解压后的目录

cd postgresql-9.0.3/

4、查看INSTALL

INSTALL文件中Short Version部分解释了如何安装postgresql的命令,Requirements部分描述了安装postgresql所依赖的lib,比较长,先 configure试一下,如果出现error,那么需要检查是否满足了Requirements的要求。

Short Version

./configuregmakesugmake installadduser postgresmkdir /usr/local/pgsql/datachown postgres /usr/local/pgsql/datasu - postgres/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &/usr/local/pgsql/bin/createdb test/usr/local/pgsql/bin/psql test

5、执行INSTALL文件中Short Version的命令,开始编译安装postgrepsql数据库。

./configure   --prefix=/usr/local/pgsql --with-perl --with-python --with-libxml --with-libxslt

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 isnt looking in the proper directory.

yum install -y readline-devel

(sudo apt-get install libreadline5-dev && sudo apt-get install zlib1g-dev)

configure: error: library ‘xslt‘ is required for XSLT support

yum install libxslt libxslt-devel

configure: error: header file  is required for Python

yum install python python-devel

configure: error: could not determine flags for linking embedded Perl.

yum install perl-ExtUtils-Embed

安装readline包之后,重新configure,成功。

6、make

7、make install

8、添加用户postgres

useradd postgres

9、创建数据库文件存储文件夹

mkdir /usr/local/pgsql/data

#mkdir /data/pgsql

10、改变先前创建的data目录的文件夹的权限

chown postgres /usr/local/pgsql/data

#chown postgres  /data/pgsql

11、切换用户

su - postgres

12、绑定数据库文件存储目录

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

#export PATH=$PATH:/usr/local/pgsql/bin/

#/usr/local/pgsql/bin/initdb -D  /data/pgsql

13、启动数据库

/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1

[1] 18635

#/usr/local/pgsql/bin/postgres -D  /data/pgsql  >logfile 2>&1

[1] 18635

14、创建数据库test

/usr/local/pgsql/bin/createdb test

15、连接到test数据库

/usr/local/pgsql/bin/psql test

psql (9.0.3)

Type "help" for help.

test=#

16、创建表table1

test=# create table table1 (

test(# id integer

test(# );

CREATE TABLE

test=#

17、向table1表中插入一条记录

test=# insert into table1 values(1);

INSERT 0 1

18、查询刚刚插入的记录

test=# select * from table1;

id

----

1

0494e829016667bca096101c4f833891.png

三、客户端安装

因为对于我们来说postgre应用范围很小,单纯去学他的命令行,对于我们的时间管理不是太合适。

所以我们可以去下载一个客户端,进行一些简单的操作,推荐navicat官网去下载postgresql客户端;

----------------------------------------------------------------------------------------

### 安装 PostgreSQL-devel 16.8 版本的步骤 在 Linux 系统中安装特定版本的 PostgreSQL 开发库(如 PostgreSQL-devel 16.8),需要根据操作系统类型选择合适的方法。以下是针对 CentOS 和 AlmaLinux 的详细说明。 #### 1. 在 CentOS 7 上安装 PostgreSQL-devel 16.8 CentOS 7 默认的软件仓库可能不包含最新的 PostgreSQL 版本,因此需要添加 PostgreSQL 的官方 Yum 仓库。 - 添加 PostgreSQL 的官方 Yum 仓库: ```bash sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm ``` - 安装 PostgreSQL 16 的开发库: ```bash sudo yum install -y postgresql16-devel ``` 这将安装 PostgreSQL 16 的开发库,包括头文件和库文件,用于编译与 PostgreSQL 相关的应用程序[^1]。 #### 2. 在 AlmaLinux 9.4 上安装 PostgreSQL-devel 16.8 AlmaLinux默认仓库可能不包含 PostgreSQL 16 的开发库,因此需要启用额外的仓库,例如 EPEL 和 CodeReady Builder。 - 启用 EPEL 和 CodeReady Builder 仓库: ```bash sudo dnf install -y epel-release sudo dnf config-manager --set-enabled crb ``` - 添加 PostgreSQL 的官方 Yum 仓库: ```bash sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm ``` - 安装 PostgreSQL 16 的开发库: ```bash sudo dnf install -y postgresql16-devel ``` 如果遇到依赖问题(如缺少 `perl(IPC::Run)`),可以通过以下方式解决: ```bash sudo dnf install -y perl-IPC-Run ``` 然后重新尝试安装 `postgresql16-devel`[^2]。 #### 3. 源码方式安装 PostgreSQL-devel 16.8 如果无法通过包管理器安装,可以选择从源码编译安装- 下载 PostgreSQL 16.8 的源码包: ```bash wget https://ftp.postgresql.org/pub/source/v16.8/postgresql-16.8.tar.gz ``` - 解压并编译安装: ```bash tar -xzf postgresql-16.8.tar.gz cd postgresql-16.8 ./configure make sudo make install ``` - 安装开发库: ```bash sudo make install-world ``` 这将安装 PostgreSQL 的开发库到系统中,通常位于 `/usr/local/pgsql/include` 和 `/usr/local/pgsql/lib` 目录下[^3]。 #### 注意事项 - 确保系统已安装必要的依赖包,例如 `gcc`、`make` 和 `openssl-devel`。 - 如果需要将 PostgreSQL 的路径添加到环境变量中,可以编辑 `/etc/profile` 或 `~/.bashrc` 文件: ```bash export PATH=/usr/local/pgsql/bin:$PATH export LD_LIBRARY_PATH=/usr/local/pgsql/lib:$LD_LIBRARY_PATH ``` ### 示例代码 以下是一个简单的 C 程序示例,展示如何使用 PostgreSQL 的开发库连接数据库: ```c #include <libpq-fe.h> #include <stdio.h> int main() { const char *conninfo = "dbname=testdb user=postgres password=secret"; PGconn *conn = PQconnectdb(conninfo); if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(conn)); PQfinish(conn); return 1; } printf("Connected to database successfully.\n"); PQfinish(conn); return 0; } ``` 编译该程序时需要链接 PostgreSQL 的库: ```bash gcc -o test_db test_db.c -I/usr/local/pgsql/include -L/usr/local/pgsql/lib -lpq ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值