Install PostgreSQL 10 on CentOS 7
Content of post.
At this article we’ll see how to install PostgreSQL 10 on CentOS 7, if you’re hurry or not have interest about installation details go to the end of this article and use the script that contains all necessary commands, so let’s beginning.
The goal this tutorial is to install and to configure a instance of PostgreSQL to be used on development environment, many configurations necessary to production environment are ignored.
Installation
Before install we’ll need to do CentOS ignore the PostgreSQL of your repositories, for do it execute the text below in CentOS terminal.
sudo sed -e '/exclude=postgresql\*/d' \
-e '/^\[base\]$\|^\[updates\]$/a exclude=postgresql\*' \
-i /etc/yum.repos.d/CentOS-Base.repo
Taken due precautions we can advance with the installation, notice that we’re using official PostgreSQL packages.
sudo yum install -y https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm && \
sudo yum install -y postgresql10 postgresql10-server
Now is necessary to run the setup to create the necessary files.
sudo /usr/pgsql-10/bin/postgresql-10-setup initdb
Access configurations
Firstly we’ll allow access for remote clients.
sudo sed -e "/listen_addresses = '\*'/d" \
-e "/#listen_addresses = 'localhost'/a listen_addresses = '\*'" \
-i /var/lib/pgsql/10/data/postgresql.conf
And now we’ll allow the remote access for all user, databases and IP addresses.
sudo sed 's/host.*all.*all.*127.0.0.1.*ident/host all all all md5/' \
-i /var/lib/pgsql/10/data/pg_hba.conf
Initialization
After the installation step, we’ll to configure auto-start on CentOS boot and start PostgreSQL.
sudo systemctl enable postgresql-10.service && \
sudo systemctl start postgresql-10.service
If happen some problem, you can to check what went wrong running the command below.
sudo systemctl status postgresql-10.service
Create database and user
With PostgreSQL running, it’s time to finally to create an user and a database, on this case the user-name and the password will to be the value return to $USER variable, that is the user of the CentOS that ran the command, but you is free to use the values that you wish.
sudo -u postgres createdb $USER
sudo -u postgres psql -U postgres -d $USER -c "CREATE ROLE $USER WITH SUPERUSER LOGIN PASSWORD '$USER';"
To confirm if all went right, run the command below and you’ll should access the PostgreSQL CLI without to need inform any parameter.
psql
Case you can’t access, I suggest to you uninstall completely the PostgreSQL with command below and rehash the tutorial from the begin.
sudo yum remove -y pgdg-centos10 postgresql10 postgresql10-server && \
sudo rm -rf /var/lib/pgsql/10
Only for the hurried
If you’re hurry or not worried at installation details, the script below is a compiled with all commands presents in this article, run it and PostgreSQL will be installed.
If you followed the step-by-step not need to run this script.
sudo sed -e '/exclude=postgresql\*/d' \
-e '/^\[base\]$\|^\[updates\]$/a exclude=postgresql\*' \
-i /etc/yum.repos.d/CentOS-Base.repo && \
sudo yum install -y https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm && \
sudo yum install -y postgresql10 postgresql10-server && \
sudo /usr/pgsql-10/bin/postgresql-10-setup initdb && \
sudo sed -e "/listen_addresses = '\*'/d" \
-e "/#listen_addresses = 'localhost'/a listen_addresses = '\*'" \
-i /var/lib/pgsql/10/data/postgresql.conf && \
sudo sed 's/host.*all.*all.*127.0.0.1.*ident/host all all all md5/' \
-i /var/lib/pgsql/10/data/pg_hba.conf && \
sudo systemctl enable postgresql-10.service && \
sudo systemctl start postgresql-10.service && \
sudo -u postgres createdb $USER && \
sudo -u postgres psql -U postgres -d $USER -c "CREATE ROLE $USER WITH SUPERUSER LOGIN PASSWORD '$USER';"
Wrapping up
We reached at end of this tutorial, now you have installed and configured on your CentOS a PostgreSQL to be used in development environments, see you next time. =]