Linux下安装部署postgreSQL详细步骤

本文详细描述了如何在LinuxCentOS8环境下安装PostgreSQL11.1,包括下载、依赖安装、源码编译、数据目录设置、环境变量配置、权限管理和服务自启动等步骤,确保了数据库的完整部署和安全性。
摘要由CSDN通过智能技术生成

Linux下安装部署postgreSQL详细步骤

安装环境

#postgresql-11.1
#centos-8.0
#确认linux系统可以正常连接网络,因为在后面需要添加依赖包

pg数据库安装包下载

下载地址:http://www.postgresql.org/ftp/source/

#选择要安装的版本进行下载

安装依赖包

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake

安装postgres

#在根目录下新建pgsql文件夹,并将pgsql的压缩包移入
[root@localhost pgsql]# pwd
/pgsql
[root@localhost pgsql]# ls
postgresql-11.1.tar.gz
#解压压缩包
tar -zxvf postgresql-11.1.tar.gz
#进入解压后的文件夹 
[root@localhost pgsql]# cd postgresql-11.1
[root@localhost postgresql-11.1]# ls
aclocal.m4  configure     contrib    doc             HISTORY  Makefile  src
config      configure.in  COPYRIGHT  GNUmakefile.in  INSTALL  README

#编译postgresql源码
./configure --prefix=/pgsql/postgresql
make
make install

#创建用户组postgres并创建用户postgres 
groupadd postgres
useradd -g postgres postgres
id postgres
#创建postgresql数据库的数据主目录并修改文件所有者
#这个数据库主目录是随实际情况而不同,这里我们的主目录是在/pgsql/postgresql/data目录
[root@localhost postgresql-11.1]# cd /pgsql/postgresql
[root@localhost postgresql]# mkdir data
[root@localhost postgresql]# chown postgres:postgres data

配置环境变量

[root@localhost home]# cd /home/postgres/
[root@localhost postgres]# ls -al
总用量 20
drwx------  2 postgres postgres 4096  48 11:13 .
drwxr-xr-x. 4 root     root     4096  48 11:13 ..
-rw-r--r--  1 postgres postgres   75  110  2020 .bash_logout
-rw-r--r--  1 postgres postgres   71  319  2020 .bash_profile
-rw-r--r--  1 postgres postgres  138  110  2020 .bashrc
[root@localhost postgres]# vi .bash_profile
#添加以下内容
export PGHOME=/pgsql/postgresql
 
export PGDATA=/pgsql/postgresql/data
 
PATH=$PATH:$HOME/bin:$PGHOME/bin

#保存,退出vi。执行以下命令,使环境变量生效
[root@localhost postgres]# source .bash_profile

切换用户到postgres并使用initdb初使用化数据库

[root@localhost postgres]# su - postgres

[postgres@localhost ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /pgsql/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /pgsql/postgresql/data -l logfile start
#可以看到 /pgsql/postgresql/data已经有文件了
[postgres@localhost ~]$ cd /pgsql/postgresql/data/
[postgres@localhost data]$ ls
base          pg_dynshmem    pg_logical    pg_replslot   pg_stat      pg_tblspc    pg_wal                postgresql.conf
global        pg_hba.conf    pg_multixact  pg_serial     pg_stat_tmp  pg_twophase  pg_xact
pg_commit_ts  pg_ident.conf  pg_notify     pg_snapshots  pg_subtrans  PG_VERSION   postgresql.auto.conf
[postgres@localhost data]$ 

配置服务

#修改/pgsql/postgresql/data目录下的两个文件。

#postgresql.conf   配置PostgreSQL数据库服务器的相应的参数。  
[postgres@localhost data]$ vi postgresql.conf
#其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。
     57 # - Connection Settings -
     58 
     59 listen_addresses = '*'          # what IP address(es) to listen on;
     60                                         # comma-separated list of addresses;
     61                                         # defaults to 'localhost'; use '*' for all
     62                                         # (change requires restart)
     63 #port = 5432                            # (change requires restart)
#pg_hba.conf        配置对数据库的访问权限。
#找到最下面这一行 ,这样局域网的人才能访问。
# IPv4 local connections:
host    all             all             0.0.0.0/0               trust
host    all             all             127.0.0.1/32            trust

设置PostgreSQL开机自启动

#PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。

#linux文件即为linux系统上的启动脚本
[postgres@localhost data]$ cd /pgsql/postgresql-11.1/contrib//start-scripts/
[postgres@localhost start-scripts]$ ls
freebsd  linux  macos
[postgres@localhost start-scripts]$ 
#切换为root用户,修改linux文件属性,添加X属性
[root@localhost start-scripts]# chmod a+x linux

#复制linux文件到/etc/init.d目录下,更名为postgresql
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql

#修改/etc/init.d/postgresql文件的两个变量

#prefix设置为postgresql的安装路径:/pgsql/postgresql
#PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data
#设置postgresql服务开机自启动
[root@localhost start-scripts]# cd /etc/init.d/
[root@localhost init.d]# chkconfig --add postgresql
#查看开机自启动服务设置成功。
[root@localhost init.d]# chkconfig

postgresql      0:关    1:关    2:开    3:开    4:开    5:开    6:关

关闭防火墙

systemctl stop firewalld

执行service postgresql start,启动PostgreSQL服务

service postgresql start

#查看PostgreSQL服务
[root@localhost sysconfig]# ps -ef | grep postgres
root       31500   21839  0 11:21 pts/1    00:00:00 su - postgres
postgres   31501   31500  0 11:21 pts/1    00:00:00 -bash
postgres   33980       1  0 13:50 ?        00:00:00 /pgsql/postgresql/bin/postmaster -D /pgsql/postgresql/data
postgres   33982   33980  0 13:50 ?        00:00:00 postgres: checkpointer   
postgres   33983   33980  0 13:50 ?        00:00:00 postgres: background writer   
postgres   33984   33980  0 13:50 ?        00:00:00 postgres: walwriter   
postgres   33985   33980  0 13:50 ?        00:00:00 postgres: autovacuum launcher   
postgres   33986   33980  0 13:50 ?        00:00:00 postgres: stats collector   
postgres   33987   33980  0 13:50 ?        00:00:00 postgres: logical replication launcher   
root       34026   33586  0 14:17 pts/1    00:00:00 grep --color=auto postgres

开始测试

切换为postgres用户,进入客户端

su - postgres
psql

创建数据库用户

 create user pg password 'pg';
#赋予账号权限
 ALTER ROLE pg SUPERUSER;
#新建数据库
 create database pg;
#退出
#重新登录数据库
psql -U pg -d pg 

#登录成功说明数据库安装成功
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux部署项目的具体步骤可能会因项目类型和要求的不同而有所变化,但是下面是一个通用的部署流程: 1. 准备环境: - 确保Linux系统已经安装了所需的软件和依赖项,如Web服务器(如Apache或Nginx)、数据库(如MySQL或PostgreSQL)等。 - 安装和配置所需的开发环境,如Node.js、Python等。 2. 获取项目代码: - 将项目代码从版本控制系统(如Git)中获取到Linux服务器上。可以使用命令行工具或GUI工具来完成该操作。 3. 配置项目: - 根据项目要求进行配置。这可能包括设置环境变量、数据库连接、文件路径等。 4. 安装依赖项: - 运行适当的命令(如npm install、pip install等)来安装项目所需的依赖项。这些命令会根据项目中的配置文件自动安装所需的软件包。 5. 构建项目(如果需要): - 如果项目需要构建步骤,比如编译前端代码或生成静态文件等,运行相应的构建命令。 6. 部署项目: - 将项目文件复制到Web服务器的适当目录下,以使其可以通过Web访问。这通常是将文件放置在Web服务器的根目录(如/var/www/html)或虚拟主机的目录中。 7. 配置Web服务器: - 配置Web服务器以正确地处理项目的请求。这可能涉及创建虚拟主机配置文件、配置SSL证书(如果需要使用HTTPS)等。 8. 启动项目: - 启动项目的主要服务。这可能是运行一个命令,如node app.js、python manage.py runserver等。 9. 测试项目: - 通过浏览器或其他HTTP请求工具访问项目的URL,确保项目能够正常运行并响应请求。 请注意,这只是一个通用的部署步骤指南。实际部署过程可能因项目的特定要求和技术栈而有所不同。建议查阅相关文档或参考特定框架/工具的官方文档以获取更详细和准确的指导。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪兽王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值