Home CentOS CentOS 7 Install PostgreSQL and pgAdmin using uWSGI on Centminmod CentOS 7

Install PostgreSQL and pgAdmin using uWSGI on Centminmod CentOS 7

PostgreSQL is a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.

pgAdmin is the most popular and feature rich Open Source administration and development platform for PostgreSQL, the most advanced Open Source database in the world.

Lets install PostgreSQL 11

vi /etc/centminmod/custom_config.inc

Enter

POSTGRESQL=y
LETSENCRYPT_DETECT='y'

Upgrade php using centmin.sh option 5

cmdir
./centmin.sh

This will install PostgreSQL 11

systemctl status postgresql-11
psql --version
psql (PostgreSQL) 11.7

Lets install pgAdmin4

pip3 install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v4.20/pip/pgadmin4-4.20-py2.py3-none-any.whl

Setup config file with relative paths

vi /usr/local/lib/python3.6/site-packages/pgadmin4/config_local.py

Add

LOG_FILE = '/var/log/pgadmin/pgadmin.log'
SQLITE_PATH = '/var/lib/pgadmin/pgadmin.db'
SESSION_DB_PATH = '/var/lib/pgadmin/sessions'
STORAGE_DIR = '/var/lib/pgadmin/storage'
SERVER_MODE = True
ALLOW_SAVE_PASSWORD = True

Execute setup.py to configure login for pgAdmin

python3 /usr/local/lib/python3.6/site-packages/pgadmin4/setup.py

Set correct permission to the files

chown nginx:nginx /var/lib/pgadmin/pgadmin.db
chown -R nginx:nginx /var/log/pgadmin/
chown -R nginx:nginx /var/lib/pgadmin/

Create pgadmin.ini file

mkdir -p /home/nginx/domains/uwsgi
vi /home/nginx/domains/uwsgi/pgadmin.ini

Add

[uwsgi]
uid             = nginx
gid             = nginx
chdir           = /usr/local/lib/python3.6/site-packages/pgadmin4/
wsgi-file       = /usr/local/lib/python3.6/site-packages/pgadmin4/pgAdmin4.wsgi
plugins         = python3
master          = true
processes       = 1
socket          = /run/uwsgi/pgadmin.sock
chown-socket    = nginx:nginx
chmod-socket    = 660
vacuum          = true

Create uwsgi systemd service

vi /etc/systemd/system/uwsgi.service

Add

[Unit]
Description=uWSGI Emperor service

[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /run/uwsgi; chown nginx:nginx /run/uwsgi'
ExecStart=/usr/local/bin/uwsgi --emperor /home/nginx/domains/uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target

Start and enable uwsgi services

systemctl start uwsgi
systemctl enable uwsgi

Lets configure Nginx as reverse proxy for pgadmin4

First create domain name with centmin.sh using option 2

cmdir
./centmin.sh

Copy staticfiles.conf to staticfiles-pgadmin4.conf

cat /usr/local/nginx/conf/staticfiles.conf > /usr/local/nginx/conf/staticfiles-pgadmin4.conf

Open staticfiles-pgadmin4.conf

vi /usr/local/nginx/conf/staticfiles-pgadmin4.conf

Find

location ~ /.well-known { location ~ /.well-known/acme-challenge/(.*) { more_set_headers    "Content-Type: text/plain"; } }

Delete everything after this line and save file

Open your vhost file for the domain name your created. In my example I created site.bullten.work

vi /usr/local/nginx/conf/conf.d/site.bullten.work.ssl.conf

on to add the line below

upstream pgadmin {
      server unix:///run/uwsgi/pgadmin.sock;
}

So it will look like

Find

location / {

Under it Add

   include uwsgi_params;
   uwsgi_pass pgadmin;

So it will look like as shown in the image below

Also Find

include /usr/local/nginx/conf/staticfiles.conf;

Replace With

include /usr/local/nginx/conf/staticfiles-pgadmin4.conf

Save the file and restart nginx

nprestart

Now lets configure PostgreSQL so it can be added as server in pgAdmin

vi /var/lib/pgsql/11/data/pg_hba.conf

Find

local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident

Change peer and ident to trust. So it will look like as below

local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust

Restart PostgreSQL

systemctl restart postgresql-11

Now lets change postgres password

su - postgres
psql postgres
\password postgres

After this is done we will open pgAdmin interface with the domain name you created above and add out PostgreSQL server.

Now enter anything in Name and click on Connection tab after it

Under hostname/address enter 127.0.0.1. Change username to postgres and password to the password you changed above using command \password postgres. Click on save and you will see your server has been added to it.

Must Read

Routing Domain Name in Haproxy

I hope you are following the below guide. Now suppose you want to route domain name to open specific backend that have...

Enable Logging in Haproxy

HAProxy can emit log message for processing by a syslog server. This is compatible with familiar syslog tools like Rsyslog, as well...

Getting real IP in Nginx when behind cloudflare Haproxy

Create a file named cloudflare_ips.ls and in that add all the cloudflare IP's vi /etc/haproxy/cloudflare_ips.ls 173.245.48.0/20 103.21.244.0/22 103.22.200.0/22 103.31.4.0/22 141.101.64.0/18 108.162.192.0/18 190.93.240.0/20 188.114.96.0/20 197.234.240.0/22 198.41.128.0/17 162.158.0.0/15 104.16.0.0/12 172.64.0.0/13 131.0.72.0/22 2400:cb00::/32 2606:4700::/32 2803:f800::/32 2405:b500::/32 2405:8100::/32 2a06:98c0::/29 2c0f:f248::/32

Getting Real IP in HaproxyNginx configuration

Well its a little configuration can get real to nginx when haproxy is set as reverse proxy. Follow this...

Haproxy as Single Point to Failure Node With Glusterfs and MariaDB Maxscale Cluster

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It...