资讯详情

Linux下PostgreSQL安装部署详细步骤

目录

一、安装环境

二、pg下载数据库安装包

三、安装依赖包

四、安装postgres

五、创建用户组postgres并创建用户postgres

六、创建postgresql数据库的数据主目录并修改文件所有者

七、配置环境变量

八、切换用户到达postgres并使用initdb数据库的初始使用

九、配置服务

十、设置PostgreSQL开机自启动

开始测试

一、安装环境 postgresql-11.1 CentOS-6.4 注意:确认linux由于后面需要添加依赖包,系统可以正常连接网络。 二、pg下载数据库安装包 下载地址:http://www.postgresql.org/ftp/source/

下载要安装的版本:

三、安装依赖包 在要安装postgresql数据库的Linux服务器(hostname:weekend02)执行下列安装命令所需的依赖包:

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@weekend02 pgsql]# ls postgresql-11.1.tar.gz [root@weekend02 pgsql]# pwd //pgsql 2.解压压缩包

[root@weekend02 pgsql]# tar -zxvf postgresql-11.1.tar.gz 3.解压后进入文件夹

[root@weekend02 pgsql]# cd postgresql-11.1 [root@weekend02 postgresql-11.1]# ls aclocal.m4 configure contrib doc HISTORY Makefile src config configure.in COPYRIGHT GNUmakefile.in INSTALL README 4、编译postgresql源码

[root@weekend02 postgresql-11.1]# ./configure --prefix=/pgsql/postgresql 选项 描述 –prefix=prefix 安装到prefix指向目录;默认为/usr/local/pgsql –bindir=dir 安装应用程序到dir;默认为prefix/bin –with-docdir=dir 安装文档到dir;默认为prefix/doc –with-pgport=port 默认设置服务器端网络连接服务TCP端口号 –with-tcl 为服务端提供Tcl支持存储过程 –with-perl 为服务端提供Perl存储过程支持 –with-python 为服务端提供Python支持存储过程 [root@weekend02 postgresql-11.1]# make [root@weekend02 postgresql-11.1]# make install 至此,已完成postgreql安装。进入//pgsql/postgresql安装后的目录可以看到postgresql的文件。

[root@weekend02 postgresql]# ls bin include lib share 五、创建用户组postgres并创建用户postgres [root@weekend02 postgresql-11.1]# groupadd postgres [root@weekend02 postgresql-11.1]# useradd -g postgres postgres [root@weekend02 postgresql-11.1]# id postgres uid=501(postgres) gid=501(postgres) 组=501(postgres) 六、创建postgresql数据库的数据主目录并修改文件所有者 这个数据库的主目录因实际情况而异。这里我们的主目录是/pgsql/postgresql/data目录下:

[root@weekend02 postgresql-11.1]# cd /pgsql/postgresql [root@weekend02 postgresql]# mkdir data [root@weekend02 postgresql]# chown postgres:postgres data [root@weekend02 postgresql]# ls -al 总用量 28 drwxr-xr-x. 7 root root 4096 12月 15 23:39 . drwxr-xr-x. 4 root root 4096 12月 15 23:14 .. drwxr-xr-x. 2 root root 4096 12月 15 23:14 bin drwxr-xr-x. 2 postgres postgres 4096 12月 15 23:39 data drwxr-xr-x. 6 root root 4096 12月 15 23:14 include drwxr-xr-x. 4 root root 4096 12月 15 23:14 lib drwxr-xr-x. 6 root root 4096 12月 15 23:14 share 七、配置环境变量 进入home/postgres可以看到目录.bash_profile文件。

[root@weekend02 postgresql]# cd /home/postgres [root@weekend02 postgres]# ls -al 总用量 28 drwx------. 4 postgres postgres 4096 12月 15 23:37 . drwxr-xr-x. 5 root root 4096 12月 15 23:19 .. -rw-r--r--. 1 postgres postgres 18 5月 11 2012 .bash_logout -rw-r--r--. 1 postgres postgres 178 12月 15 23:37 .bash_profile -rw-r--r--. 1 postgres postgres 124 5月 11 2012 .bashrc drwxr-xr-x. 2 postgres postgres 4096 11月 12 2010 .gnome2 drwxr-xr-x. 4 postgres postgres 4096 9月 29 05:12 .mozilla [root@weekend02 postgres]# 编辑修改.bash_profile文件。

[root@weekend02 postgres]# vi .bash_profile 添加以下内容。

export PGHOME=/pgsql/postgresql export PGDATA=/pgsql/postgresql/data PATH=$PATH:$HOME/bin:$PGHOME/bin 保存,退出vi。执行下列命令,使环境变量生效

[root@weekend02 postgres]# source .bash_profile 八、切换用户到达postgres并使用initdb数据库的初始使用 [root@weekend02 postgres]# su - postgres [postgres@weekend02 ~]$ 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. &nbs; 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@weekend02 /]$ cd /pgsql/postgresql/data   [postgres@weekend02 data]$ ls base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf pg_commit_ts  pg_multixact   pg_stat       PG_VERSION pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal pg_hba.conf   pg_replslot    pg_subtrans   pg_xact 九、配置服务 修改/pgsql/postgresql/data目录下的两个文件。

postgresql.conf   配置PostgreSQL数据库服务器的相应的参数。  

pg_hba.conf        配置对数据库的访问权限。

[postgres@weekend02 data]$ vi postgresql.conf    listen_addresses = '*'                  # what IP address(es) to listen on;                                         # comma-separated list of addresses;                                         # defaults to 'localhost'; use '*' for all                                         # (change requires restart) #port = 5432                            # (change requires restart) 其中,参数“listen_addresses”表示监听的IP地址,默认是在localhost处监听,也就是127.0.0.1的ip地址上监听,只接受来自本机localhost的连接请求,这会让远程的主机无法登陆这台数据库,如果想从其他的机器上登陆这台数据库,需要把监听地址改为实际网络的地址,一种简单的方法是,将行开头的#去掉,把这个地址改为*,表示在本地的所有地址上监听。

[postgres@weekend02 data]$ vi 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@weekend02 pgsql]$ cd /pgsql/postgresql-11.1/contrib/start-scripts [postgres@weekend02 start-scripts]$ ls freebsd  linux  macos 1)切换为root用户,修改linux文件属性,添加X属性

[root@weekend02 start-scripts]# chmod a+x linux 2) 复制linux文件到/etc/init.d目录下,更名为postgresql

[root@weekend02 start-scripts]# cp linux /etc/init.d/postgresql 3)修改/etc/init.d/postgresql文件的两个变量

prefix设置为postgresql的安装路径:/pgsql/postgresql

PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data

4)设置postgresql服务开机自启动

[root@weekend02 init.d]# chkconfig --add postgresql 查看开机自启动服务设置成功。

[root@weekend02 init.d]# chkconfig   postgresql         0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭 5)编辑/etc/sysconfig/iptables文件开放5432端口。

[root@weekend02 sysconfig]# cd /etc/sysconfig [root@weekend02 sysconfig]# vi iptables 添加以下内容

-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT 重启服务

[root@weekend02 sysconfig]# /etc/init.d/iptables restart iptables:清除防火墙规则:                                 [确定] iptables:将链设置为政策 ACCEPT:filter           [确定] iptables:正在卸载模块:                                     [确定] iptables:应用防火墙规则:                                 [确定] 查看端口是否开放

[root@weekend02 sysconfig]# /sbin/iptables -L -n Chain INPUT (policy ACCEPT) target     prot opt source               destination          ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED  ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:5432  ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22  REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited    Chain FORWARD (policy ACCEPT) target     prot opt source               destination          REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited    Chain OUTPUT (policy ACCEPT) target     prot opt source               destination    6)执行service postgresql start,启动PostgreSQL服务

[root@weekend02 init.d]# service postgresql start Starting PostgreSQL: ok 查看PostgreSQL服务

[root@weekend02 init.d]# ps -ef | grep postgres root      12040   3014  0 Dec15 pts/0    00:00:00 su - postgres postgres  12041  12040  0 Dec15 pts/0    00:00:00 -bash postgres  12177      1  0 00:29 ?        00:00:00 /pgsql/postgresql/bin/postmaster -D /pgsql/postgresql/data postgres  12179  12177  0 00:29 ?        00:00:00 postgres: checkpointer                                     postgres  12180  12177  0 00:29 ?        00:00:00 postgres: background writer                                postgres  12181  12177  0 00:29 ?        00:00:00 postgres: walwriter                                        postgres  12182  12177  0 00:29 ?        00:00:00 postgres: autovacuum launcher                              postgres  12183  12177  0 00:29 ?        00:00:00 postgres: stats collector                                  postgres  12184  12177  0 00:29 ?        00:00:00 postgres: logical replication launcher                     root      12198  12132  0 00:30 pts/0    00:00:00 grep postgres 十一、开始测试 切换为postgres用户,进入客户端:

$ su - postgres $ psql 创建数据库用户

赋予账号权限

新建数据库 退出

[postgres@weekend02 ~]$ psql  psql (11.1) Type "help" for help.   postgres=# \l                                   List of databases    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges    -----------+----------+----------+-------------+-------------+-----------------------  postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |   template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +            |          |          |             |             | postgres=CTc/postgres  template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +            |          |          |             |             | postgres=CTc/postgres (3 rows)   postgres=# create user pg password 'pg'; CREATE ROLE postgres=# ALTER ROLE pg SUPERUSER; ALTER ROLE postgres=# create database pg; CREATE DATABASE postgres=# \q 重新登录数据库 输入密码 显示数据库

[postgres@weekend02 ~]$ psql -U pg -d pg -h weekend02 psql (11.1) Type "help" for help.   pg=# \l                                   List of databases    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges    -----------+----------+----------+-------------+-------------+-----------------------  pg        | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |   postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |   template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +            |          |          |             |             | postgres=CTc/postgres  template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +            |          |          |             |             | postgres=CTc/postgres (4 rows)   pg=#  说明数据库安装成功。 ———————————————— 版权声明:本文为CSDN博主「皮哥四月红」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/weixin_43230682/article/details/108403642

标签: 压差传感器dpt0100u1

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台