How to Install PostgreSQL on CentOS 7

PostgreSQL is a powerful and open source relational database platform. It’s scalable across multiple platforms and is a widely used and well-loved tool. In this tutorial, we’ll show you how to install PostgreSQL on CentOS 7!

How Does PostgreSQL work?

PostgreSQL uses SQL for relational queries and JSON for non-relational queries. One advantage of using PostgreSQL is its immense community support! Its open source nature means that a lot of developers contribute to this utility’s growth.

PostgreSQL is scalable, reliable and accompanied by advanced optimization features. In most cases, people assume advanced optimization and data types are supported by only commercial databases like SQL Server and Oracle. Contrary to this belief, PostgreSQL provides all of this and many more advanced features, definitely making it a worthwhile addition to your VPS.

PostgreSQL is extremely simple to start using and master.

Here we will demonstrate how to install PostgreSQL on CentOS 7.

You can install PostgreSQL on CentOS 7 in using one of two methods:

  1. Install PostgreSQL from existing CentOS repositories
  2. Install from the PostgreSQL repository

Let us check each of this method in more detail:

First Method – Install PostgreSQL on CentOS 7 using the CentOS repositories

The CentOS 7 repository contains PostgreSQL. Note that it may not have the latest version of PostgreSQL. At the time of writing the repository hosts PostgreSQL version 9.2.15.

1. Access Your Server

Remember, before starting to install PostgreSQL on CentOS 7, we need to access our VPS server with SSH. Check out our PuTTY tutorial if you’re having trouble

2. Install PostgreSQL on CentOS 7

It is simple to install PostgreSQL from CentOS 7 repositories. Start with the following command:

sudo yum install postgresql-server postgresql-contrib

This might take some time to complete.

3. Initialize the Database

Once the installation is done, you can initialize the database using the below command:

sudo postgresql-setup initdb

4. Start the Database

After initializing the database, you can start the database using:

sudo systemctl start postgresql

5. (Optional) Enable PostgreSQL

This completes our database installation and initialization. If required you can configure PostgreSQL to start on every system reboot automatically. 

sudo systemctl enable postgresql          

三.配置使用

1. 修改用户密码

复制代码

#yum安装postgresql,默认会建一个名为”postgres”的系统账号,用于执行PostgreSQL;
[root@psql_master ~]# su - postgres

#切换用户后,提示符变更为“-bash-4.2$”;
#同时数据库中也会生成一个名为”postgres”的数据库用户,且密码已自动生成;
#PostgreSQL在数据库用户同名的系统账号下登录免密;
-bash-4.2$ psql -U postgres

#进入数据库后修改密码;
postgres=# alter user postgres with password 'postgres@123'

复制代码

2. 允许远程访问

#配置文件中,默认只能本机访问postgresql;
#修改listen_addresses = 'localhost'为listen_addresses = '*',允许所有远程访问;
#修改配置文件需要重启服务。
[root@psql_master ~]# sed -i "s|#listen_addresses = 'localhost'|listen_addresses = '*'|g" /var/lib/pgsql/9.6/data/postgresql.conf

3. 主机认证

复制代码

#在第82行之后,”IPv4 local connections”下新增允许的客户端;
#“host” 代表主机类型,第一个“all”代表db ,第二个“all”代表user ,“172.29.3.67/32” 代表client ip,“trust”代表认证方式;
#认证方式除“trust”外,还有“peer”, “ident”, “md5”, “password”等,具体可参考pg-hba文件: https://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html
#修改pg.hba文件需要重启服务。
[root@psql_master ~]# vim /var/lib/pgsql/9.6/data/pg_hba.conf
host    all             all             172.29.3.67/32          trust

复制代码

4. 设置环境变量

[root@psql_master ~]# vim /etc/profile
export PATH=$PATH:/usr/pgsql-9.6/bin

[root@psql_master ~]# source /etc/profile

5. 重启服务

[root@psql_master ~]# systemctl restart postgresql-9.6

6. iptables

#postgresql默认开启tcp5432端口
[root@psql_master ~]# vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

[root@psql_master ~]# service iptables restart

四.使用验证

1. 查看端口

[root@psql_master ~]# netstat -tunlp

2. 简单使用

1)创建用户

postgres=# create user postuser1 with password 'user1@123';

2)创建数据库

#同时指定数据库的所有者
postgres=# create database postdb1 owner postuser1;

3)数据库赋权

#未赋权则账户只能登录控制台
postgres=# grant all privileges on database postdb1 to postuser1;

4)登录新建数据库

#在操作系统层使用新建的账号登录新建的数据库,登录后提示符为“postdb1=>”;
#如果在postgres账户下直接使用“postgres=# \c postdb1;”登录,则登录用户依然是postgres,
-bash-4.2$ psql -U postuser1 -d postdb1 -h 127.0.0.1 -p 5432

5)创建表

postdb1=> create table tb1(
          id int primary key,
          name VARCHAR(20), 
          salary real
          );

6)插入数据

postdb1=> insert into tb1(
          id, name, salary)
          values(
          101, 'Mike', 5000.00
          );

7)查询

postdb1=>select * from tb1;

猜你喜欢

转载自blog.csdn.net/jsboy123/article/details/107542122