Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install PostgreSQL on Linux system

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to install PostgreSQL on Linux systems. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

About PostgreSQL: PostgreSQL is a powerful open source object-relational database system that uses and extends the SQL language and combines many capabilities to securely store and extend the most complex data workloads.

PostgreSQL dates back to 1986 as part of the UC Berkeley POSTGRES project and has been actively developed on the core platform for more than 30 years.

PostgresSQL has earned a reputation for its proven architecture, reliability, data integrity, powerful feature set, scalability, and the dedication of the open source community behind the software to consistently provide high-performance and innovative solutions.

It has never been easier for PostgreSQL to start using PostgreSQL in all major operating systems.

Linux system installation PostgreSQL detailed steps: 1. Check whether PostgreSQL has installed Linux- package Management-rpm Command Management-query rpm-qa | grep postgres check whether PostgreSQL has installed rpm-qal | grep postgres checks the location where PostgreSQL is installed

If already installed, use the rpm-e command to uninstall.

Rpm-e postgresql94-contrib-9.4.4-1PGDG.rhel6.x86_64 postgresql94-server-9.4.4-1PGDG.rhel6.x86_64 uninstall rpm-e postgresql94-libs-9.4.4-1PGDG.rhel6.x86_64 postgresql94- 9.4.4-1PGDG.rhel6.x86_64 uninstall

You can use rpm-qa | grep postgres to check again to see if there are any Postgres installation files. The uninstall is not complete.

two。 To install the postgresql94 version using the yum library settings, it is best to create user groups and users first:

(1) add postgres user groups:

Groupadd postgres (2) add a postgres user and set this user to belong to the postgres user group created above:

Useradd-g postgres postgres (3) modify the postgres user password: passwd postgres (here set the password to postgres)

Passwd postgres-change postgres user password yum install http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-redhat94-9.4-1.noarch.rpm using the yum library

3. You can also look for postgresql94 yum search postgresql / / installation first. There will be multiple server versions. Decide which yum-y install postgresql94-server postgresql94-contrib / / install postgresql94 to install.

Note: if there is an error, first stop the service to look at the service name chkconfig-list to find the service name of postgresql, note that the service name may have the version number, service service name stop, then use the rpm-e uninstall command, and then execute the above installation

Installation succeeded

You can use the command rpm-qal | grep postgres to view the location of the installation file, and note the location and name of the default postgresql configuration file (with version number)

View the PGDATA information in the / etc/rc.d/init.d/postgresql-9.4 file and display the line number.

Cat / etc/rc.d/init.d/postgresql-9.4 | grep-n PGDATA-n displays the line number

The figure above shows that the default database file is installed at: / var/lib/pgsql/9.4/data. If you need to change the location of the database file, you can modify it here. Here is how to change the location of the default database file. Let's see if there are any database files in this directory:

Ls-la / var/lib/pgsql/9.4/data check the contents of this directory

The figure above shows that after the postgresql installation is complete, the directory used to store the default database files is empty.

The following is important and describes how to install the postgresql database:

1. How to change the default database installation location and install postgresql database:

Linux add / remove users and user groups

1. Delete the postgres user and his file information (if previously installed)

Userdel-r postgres2, delete postgres user group (if previously installed)

Groupdel postgres (1) add postgres user group:

Groupadd postgres (2) add a postgres user and set this user to belong to the postgres user group created above:

Useradd-g postgres postgres (3) modify the postgres user password: passwd postgres (here set the password to postgres)

Passwd postgres-change the password of the postgres user

Create a directory where the database files are stored: / mnt/pgsql/data

Create a pgsql/data directory recursively with mkdir-p / mnt/pgsql/data

By default, the owners and all groups of the newly created directories / mnt/pgsql and / mnt/pgsql/data are root and need to be modified to postgres users and user groups.

Chown-R postgres:postgres / mnt/pgsql ll / mnt chown-R postgres:postgres / mnt/pgsql/data ll / mnt/pgsql to see if it has been changed to postgres users and user groups.

The next step is to change the default database file storage path:

Modify the PGDATA and PGLOG in the configuration file (/ etc/rc.d/init.d/postgresql-9.4) of the postgresql database to the directory we created: / mnt/pgsql:

PGDATA=/mnt/pgsql/data PGLOG=/mnt/pgsql/pgstartup.log

After saving, continue to query the location of the initdb file:

Find /-name initdb finds the location where initdb is installed

Specify the location and encoding of the database, and initialize the database:

/ usr/pgsql-9.4/bin/initdb-E UNICODE-D / mnt/pgsql/data

Note: the above error indicates that Postgresql cannot be initialized as root, but will be run as postgres, so create the linux user postgres and group postgres above.

Switch postgres users to initialize the database

Su-postgres switching user / usr/pgsql-9.4/bin/initdb-E UNICODE-D / mnt/pgsql/data-initializing database cd / mnt/pgsql/data switching directory ll query directory contents after database initialization is successful

Edit the postgresql.conf file and modify the listening address and port parameters that the database receives by default:

Cat postgresql.conf | grep-n listen_addresses finds the location of listen_addresses in the postgresql.conf file and displays the line number vi postgresql.conf edits the postgresql.conf file to query the value of listen_addresses:

Modify the value of listen_addresses before:

Remove the comment on line 59 and change listen_addresses = 'localhost' to listen_addresses =' *'. The following figure shows the value of the modified listen_addresses:

Next, continue to modify the pg_hba.conf file to tell the database server what kind of clients it will allow to connect to itself:

Vi pg_hba.conf-modify the source file before the modification of the postgresql service connection file:

Add a line below line 86 to allow any client to access itself with the correct username and password:

Host all all 0.0.0.0/0 trust

# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust or peer # IPv4 local connections: host all all 127.0.0.1 host all all 32 trust host all all 0. 0.0.0/0 password # IPv6 local connections: host all all:: 1/128 password # Allow replication connections from localhost By a user with the # replication privilege. # local replication postgres peer # host replication postgres 127.0.0.1/32 ident # host replication postgres:: 1/128 ident

Note: if you set trust, you can log in to the server directly using psql-U postgres locally. If you set peer, you can log in directly to the server using psql-h 127.0.0.1-d postgres-U postgres.

Exit-returns the root user

Chkconfig-- list looks at all the services and finds the service name of postgre

The default postgresql-9.4 is not self-starting. We need to set the postgresql-9.4 service to self-starting service:

Chkconfig postgresql-9.4 on setup service self-startup chkconfig-- list

Service postgresql-9.4 status View Service status service postgresql-9.4 start starts the postgresql-9.4 service

There are three ways to see if postgresql is running

Ps-ef | grep postgres checks the process information of postgres netstat-tpnl | grep 5432 checks whether the port number 5432 of postgres has been turned on service postgresql-9.4 status to check the service status

Start connecting to the postgresql database:

Psql-U postgres / / Connect pgsql server psql-h 127.0.0.1-d postgres-U postgres / / Connect pgsql server

The picture above indicates that the connection to the database is successful.

After modifying the configuration file pg_hba.conf, log in to the database and refresh the updated configuration command:

Select pg_reload_conf ()

Show data_directory

Set the password to log in to the database for the default login name postgres (equivalent to sa in SQLServer):

ALTER USER postgres WITH PASSWORD '1234login; / / add password select * from pg_shadow;\ Q enter to log in

Set up a firewall

Iptables-I INPUT-p tcp-- dport 22-j ACCEPT iptables-I INPUT-p tcp-- dport 5432-j ACCEPT service iptables save service iptables restart cat / etc/sysconfig/iptables

Second, install postgresql database files by default:

There is no need to create a linux user by executing the following command directly:

Service postgresql-9.4 initdb

If an error is reported during startup, for example: Data directory is not empty! [failed], it is estimated that there is a problem with the uninstall. Delete all the files under the / var/lib/pgsql directory and use the rpm-e command to uninstall and reinstall.

Cd / var/lib/pgsql/data cd / var/lib/pgsql cd / var/lib rm-rf / var/lib/pgsql ll / var/lib

Rpm-e postgresql94-contrib-9.4.3-1PGDG.rhel6.x86_64 postgresql94-server-9.4.3-1PGDG.rhel6.x86_64 postgresql94- 9.4.3-1PGDG.rhel6.x86_64 postgresql94-libs-9.4.3-1PGDG.rhel6.x86_64 Uninstall yum-y install postgresql94-server postgresql94-contrib install service postgresql-9.4 initdb initialize database

Set up the boot postgresql

Chkconfig postgresql-9.4 on

Check to see if postgresql is running

Ps-ef | grep postgres checks the process information of postgres netstat-tpnl | grep 5432 checks whether the port number 5432 of postgres has been turned on service postgresql-9.4 status to check the service status

Log in with the postgres user and change the password

Su postgres / / switching users need to rerun the postgresql service if the following error occurs. If you want psql to log in directly, restart the DB,socket file will be generated with the startup of the server.

Ps-ef | grep postgre looks up the postgre process

Kill-9 31308 service postgresql-9.4 status service postgresql-9.4 stop service postgresql-9.4 start

Su postgres / / switch user psql-U postgres / / Connect pgsql server

ALTER USER postgres WITH PASSWORD '1234 password; / / add password select * from pg_shadow

\ Q exit if the following error occurs

Need to continue to create files

Touch / home/postgres/.psql_history create this file chmod-R Grouw / home/postgres elevate the permissions of this directory chmod postgres / home/postgres/.psql_history elevate the permissions of this file .psql _ history

Modify / var/lib/pgsql/9.4/data/postgresql.conf (this file is the default installation location)

Vi / var/lib/pgsql/9.4/data/postgresql.conf modify the configuration file of postgresql

Remove the comment from line 59 and change listen_addresses = 'localhost' to listen_addresses =' *'

Modify / var/lib/pgsql/9.4/data/pg_hba.conf (this file is the default installation location)

Vi / var/lib/pgsql/9.4/data/pg_hba.conf modifies the postgresql service connection file

Line 80: peer is renamed md5, and you can connect to yourself locally.

Add a line below line 82

Host all all 192.168.6.0 host all all 24 md5 allows 192.168.0.0 network segment connections host all all 0.0.0.0Universe md5 allows all IP connections

Turn off the firewall

Service iptables stop restarts postgresql's service

Service postgresql-9.4 restart

Third, you can connect successfully by using client tools!

Navicat for PostgreSQL

Thank you for reading! This is the end of the article on "how to install PostgreSQL in Linux system". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report