Home / How To / How to install PostgreSQL and pgAdmin4 on Ubuntu 18.04 LTS

How to install PostgreSQL and pgAdmin4 on Ubuntu 18.04 LTS



pgAdmin4 is an open source PostgreSQL management tool designed for multiple PostgreSQL database versions. pgAdmin4 has been created with all the features that can be found on the PostgreSQL server. Written in python and jquery and can be installed on Windows, Mac and Linux. It provides multiple distribution models, can be installed as a desktop application or a server application running behind the web server, e.g. Apache2.

In this tutorial, we will show you the installation and configuration of pgAdmin4 & # 39; Server Mode & # 39; on Ubuntu 18.04 server. PgAdmin4 will run as a uWSGI service behind the Apache web server, and we cover both pgAdmin4 installations from the repository and from the source.

Prerequisites

  • Ubuntu 1
    8.04 Server
  • Root privileges [19659005] Basic knowledge PostgreSQL Installation

Install PostgreSQL on Ubuntu 18.04 Server

First, we will show you the basic installation and configuration The PostgreSQL database itself. It can be installed from the official PostgreSQL repository and from the standard Ubuntu repository.

To install the PostgreSQL database from the official repository, add the Postgres key and the repository by running the following commands.

  wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - 
sudo sh -c & # 39; echo "deb http: // apt.postgresql.org / pub / repos / apt /` lsb_release -cs`-pgdg main ">> / etc / apt / sources .list.d / pgdg.list & # 39;

Then update all repositories and install PostgreSQL.

  sudo apt update 
sudo apt -y install postgresql postgresql-contrib

Login after installation the default user is named & # 39; postgres & # 39; and run the command line interface & # 39; psql & # 39; PostgreSQL.

  su - postgres 
psql

Now add a new password for the Postgres user by running the following query to the psql interface.

   password postgres 
TYPE YOUR PASSWORD

Install pgAdmin4 from the archive

By default, PostgreSQL provides its own archive for installing pgAdmin4 packages.

first Add PostgreSQL Repository

Download the PostgreSQL key and add to the archive by running the following command.

  wget - quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - 
sudo sh -c & # 39; echo "deb http: // apt.postgresql.org / pub / repos / apt /` lsb_release -cs`-pgdg main ">> / etc / apt / sources .list.d / pgdg.list "

2. Install pgadmin4

Then update all available repositories and install pgAdmin4 packages with the apt command below.

  sudo apt update 
sudo apt install pgadmin4 pgadmin4-apache2-y

During the installation, you will be prompted for the email username configuration and password configuration.

Enter your own email address that will be used as the username.

Then enter the password.

When the installation is complete, open your browser and enter the server IP address as below.

http://10.9.9.15/pgadmin4/

And you will be shown the pgAdmin4 login page. Log in with your email address and password.

And you get the pgAdmin dashboard.

Install pgAdmin4 from source

To install pgAdmin4 from source, we need to install some package dependencies on the system and configure Apache web server manually to operate the pgAdmin4 uWSGI service.

– Creating Users and Directories

First of all, we want to install pgAdmin4 on the non-root user. So we have to create a new user and create some directories for pgAdmin4.

Create a new user named & # 39; ahmad & # 39; by running the following command.

  useradd -m -s / bin / bash ahmad 
passwd ahmad

Now create the pgAdmin4 log directory and data directories for "sessions" and "storage".

  mkdir -p / var / log / pgadmin4 / 
mkdir -p / var / lib / pgadmin4 / {sessions, storage}

Then you change the owner of all these directories to the "ahmad" user.

  chown -R ahmad: ahmad / var / log / pgadmin4 / / var / lib / pgadmin4 / 

– Install Dependencies

Install all package dependencies needed, e.g. Python, Python pip and virtualenv for pgAdmin4 installation by running the following commands.

  sudo apt install build-essential libssl-dev libffi-dev libgmp3-dev virtualenv python-pip libpq -dev python-dev -y 

Then install Apache2 web server and mod_wsgi.

  sudo apt install apache2 apache2-utils libapache2-mod-wsgi l ibexpat1 ssl-cert python -y 

– Creating virtual environment Python

For this guide we will install pgAdmin4 in virtual environment for python.

Login to & # 39; ahmad & # 39; user and create a new virtual python environment named & # 39; pgamdin4 & # 39;

  su - ahmad 
virtualenv pgadmin4

Now go to the directory "pgadmin4" and activate the virtual environment.

  cd pgadmin4 / 
source basket / enable [19659053] - Install pgadmin4

Inside the virtual python environment, download the pgAdmin4 whl package file.

  wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v3.6/pip/pgadmin4-3.6 -py2.py3-none-any.whl 

Then install the pip command package.

  pip install pgadmin4-3.6-py2.py3-none-any.whl 

Wait for the pgAdmin4 installation.

To run pgAdmin4 in "Server Mode" we need to add a further configuration to the python site package directory.

Go to & # 39; lib / python-version / site -packages / pgadmin4 & # 39; directory and create a new configuration file named & # 39; config_local.py & # 39 ;.

  cd lib / python2.7 / site-package / pgadmin4 / 
vim config_local.py

Paste the configuration below. [19659063] LOG_FILE = & # 39; /var/log/pgadmin4/pgadmin4.log'
SQLITE_PATH = & # 39; /var/lib/pgadmin4/pgadmin4.db'
SESSION_DB_PATH = & # 39; / var / lib / pgadmin4 / sessions & # 39;
STORAGE_DIR = & # 39; / var / lib / pgadmin4 / storage & # 39;
SERVER_MODE = True

Save and close.

Then run the script "setup.py".

  python setup.py 

And you will be prompted to manage your email address and password configuration. Enter your details and the pgAdmin installation is complete.

Note:

If you receive a message about package "psycopg2" will rename the next release, install only the binary version of the package. [19659010] pip install psycopg2-binary

Then disable the virtual python environment and change the owner of the pgAdmin4 log and data directories to the "www-data" user.

disable

  chown -R www-data: www-data / var / lib / pgadmin4 / 
chown -R www-data: www-data / var / log / pgadmin4 /

– Configure Apache2

For this guide, the Apache2 web server will be used as a reverse proxy for the uWSGI application pgAdmin4.

Go to directory & # 39; / etc / apache2 / sites-available & # 39; and create a new virtual host file named & # 39; pgadmin4.conf & # 39;

  cd / etc / apache2 / sites-available / 
vim pgadmin4.conf

Change "Server name" with your own domain name and paste it.

 
 Server name pgadmin.ahmad-labs.io
LogLevel debugging
ErrorLog $ {APACHE_LOG_DIR} /pgadmin-error.log
CustomLog $ {APACHE_LOG_DIR} /pgadmin-access.log combined

LoadModule wsgi_module modules / mod_wsgi.so
WSGIDaemonProcess pgadmin processes = 1 threads = 25 python-home = / home / ahmad / pgadmin4
WSGIScriptAlias ​​/ /home/ahmad/pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.wsgi


WSGIProcessGroup-pgAdmin
WSGIApplicationGroup% {GLOBAL}
Require everyone granted

 

Save and close.

Now test the configuration and enable the virtual pgAdmin4 hosts.

  apachectl configtest 
a2ensite pgadmin4

Make sure there is no error, then restart the Apache2 service.

  systemctl restart apache2 

Finally, pgAdmin4 is now running as a uWSGI service behind Apache browser.

Now open your browser and type the pgadmin domain name.

http://pgadmin.hakase-labs.io/

And you will get the pgAdmin4 login page as below.

Connect to the PostgreSQL database with pgAdmin4

Log in to the pgAdmin4 dashboard with your e-mail address and password.

Now click on the "Add Server" button, enter the PostgreSQL Server details you want to connect.

And click "Save".

Then you will get information about your PostgreSQL database on the left panel.

Finally, pgAdmin4 is now installed on Ubuntu 18.04


Source link