You are on page 1of 13

What is PostgreSQL

PostgreSQL is an advanced, enterprise-class, and open-source


relational database system. PostgreSQL supports both SQL
(relational) and JSON (non-relational) querying.

PostgreSQL is a highly stable database backed by more than 20 years of


development by the open-source community.

PostgreSQL is used as a primary database for many web applications as well


as mobile and analytics applications.

History of PostgreSQL
The PostgreSQL project started in 1986 at Berkeley Computer Science
Department, University of California.

The project was originally named POSTGRES, in reference to the older Ingres
database which also developed at Berkeley. The goal of the POSTGRES project
was to add the minimal features needed to support multiple data types.

In 1996, the POSTGRES project was renamed to PostgreSQL to clearly illustrate


its support for SQL. Today, PostgreSQL is commonly abbreviated as Postgres.

Since then, the PostgreSQL Global Development Group, a dedicated


community of contributors continues to make the releases of the open-source
and free database project.

Originally, PostgreSQL was designed to run on UNIX-like platforms. And then,


PostgreSQL was evolved run on various platforms such as Windows, macOS,
and Solaris.

Common Use cases of PostgreSQL


The following are the common use cases of PostgreSQL.

1) A robust database in the LAPP stack

LAPP stands for Linux, Apache, PostgreSQL, and PHP (or Python and Perl).
PostgreSQL is primarily used as a robust back-end database that powers many
dynamic websites and web applications.
2) General purpose transaction database

Large corporations and startups alike use PostgreSQL as primary databases to


support their applications and products.

3) Geospatial database

PostgreSQL with the PostGIS extension supports geospatial databases for


geographic information systems (GIS).

Language support
PostgreSQL support most popular programming languages:

• Python
• Java
• C#
• C/C+
• Ruby
• JavaScript (Node.js)
• Perl
• Go
• Tcl

PostgreSQL feature highlights


PostgreSQL has many advanced features that other enterprise-class database
management systems offer, such as:

• User-defined types
• Table inheritance
• Sophisticated locking mechanism
• Foreign key referential integrity
• Views, rules, subquery
• Nested transactions (savepoints)
• Multi-version concurrency control (MVCC)
• Asynchronous replication

The recent versions of PostgreSQL support the following features:

• Native Microsoft Windows Server version


• Tablespaces
• Point-in-time recovery

And more new features are added in each new release.


PostgreSQL is designed to be extensible. PostgreSQL allows you to define
your own data types, index types, functional languages, etc.

If you don’t like any part of the system, you can always develop a custom
plugin to enhance it to meet your requirements e.g., adding a new optimizer.

Who uses PostgreSQL


Many companies have built products and solutions based on PostgreSQL.
Some featured companies are Apple, Fujitsu, Red Hat, Cisco, Juniper Network,
Instagram, etc.

PostgreSQL Instance

# Install the repository RPM:


sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-
x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
sudo yum install -y postgresql14-server

# Optionally initialize the database and enable automatic start:


sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
sudo systemctl status postgresql-14

To install pgAdmin, run one of the following commands:


sudo yum install https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm

cat /etc/yum.repos.d/pgadmin4.repo

Install pgAdmin4 on CentOS 7 / Fedora


Then install the pgAdmin4 packageon CentOS 7 / Fedora.

Install for web mode only, suitable for server:


sudo yum install pgadmin4-web

Install for desktop mode only, suitable for Desktop:


sudo yum install pgadmin4-desktop

Install for both desktop and web modes.


sudo yum install pgadmin4

Configure pgAdmin 4 on CentOS 7 / Fedora


Now that we have pgAdmin 4 installed, let’s configure it.
1. Start and enable httpd service to start on boot
sudo systemctl start httpd
sudo systemctl enable httpd
2. You can confirm service status by running:
$ systemctl status httpd
3. Finally, if you have installed pgadmin4 or pgadmin4-web, run the web setup
script to configure the system to run in web mode:
sudo /usr/pgadmin4/bin/setup-web.sh

https://computingforgeeks.com/how-to-install-pgadmin-on-centos-fedora/
(Backup ------- Restore)

Create backup
Linux
How to Create backup databases using pg_dump
/usr/bin/pg_dump --file "/home/centos/Desktop/19-4-2022_dvdrental.backup" --
host "127.0.0.1" --port "5432" --username "dvdrental" --verbose --format=c --blobs
"dvdrental"

/usr/bin/pg_dump --file "/home/centos/Desktop/17-4-22.backup" --host "127.0.0.1"


--port "5432" --username "dvdrental" --verbose --role "dvdrental" --format=t --blobs
--encoding "UTF8" "dvdrental"

/usr/bin/pg_dump --file "/home/centos/Desktop/17-4-2022.backup" --host


"127.0.0.1" --port "5432" --username "postgres" --no-password --verbose --role
"pg_database_owner" --format=t --blobs --encoding "UTF8" "dvdrental"

Restore backup
How to restore databases using pg_restore
Tested
/usr/bin/pg_restore --host "127.0.0.1" --port "5432" --username "dvdrental" --
dbname "dvdrental" --verbose "/home/centos/Desktop/dvdrental.tar"
Old
/usr/bin/pg_restore --host "127.0.0.1" --port "5432" --username "dvdrental" --no-
password --dbname "dvdrental" --verbose "/home/centos/Desktop/dvdrental.tar"
Creating the Dump
Take the dump of a database in postgresql:
$ pg_dump -h localhost -U username -W -F t database_name > database_dump_file.tar

Explanation for the commanline options used:


-h localhost -
the database hostname, we've used localhost ( Specifies the host name of the
machine on which the server is running. )
-U username -
the database username ( Connects to the database as user username )
-W - force pg_dump to prompt for a password before connecting to the database.
-F t - Specify the format of the output ( We've used the t option to specify tar archive
file suitable for input into `pg_restore)
database_name - name of the database, of which we're taking backup
database_dump_file.tar -name of the output file

Restoring the dump


$ pg_restore -h localhost -U username -W -F t -d new_database_name database_dump_file.tar

Explanation for the commanline options used:


-h , -U, -W, -F options are the same as in pg_dump, as explained previously.
-d new_database_name - name of the database to which we're restoring the backup
( here we're restoring to new_database_name database)
database_dump_file.tar -name of the input file

Tablespace postgres
pg_default : store user Data
Pg_global : store system Data

Cast and Operators


Cast : convert Datatype to anothre Datatype
Extensions : used to wrap other objects into a single unit
Commend
sudo -i -u postgres psql
sudo su postgres
which psql
\conninfo
sudo -u postgres psql -c "show data_directory;"
show data_directory;
show all;
SELECT *, pg_tablespace_location(oid) FROM pg_tablespace;

There's no history in the database itself, if you're using psql you can use "\s" to see
your command history there. You can get future queries or other types of operations
into the log files by setting log_statement in the postgresql. conf file.
https://www.highgo.ca/2019/10/03/trace-query-processing-internals-with-
debugger/

Paths
cd /var/lib/pgsql/14
cd /var/lib/pgsql/14/data
ls
backups data initdb.log

cd /var/lib/pgsql/14/data/log
cat /var/lib/pgsql/14/data/log/postgresql-Wed.log
/var/lib/pgsql/14/backups
/usr/pgsql-14/bin
/usr/pgadmin4
How To Change The Password of a PostgreSQL User
ALTER ROLE username WITH PASSWORD 'password';
ALTER ROLE super WITH PASSWORD 'secret123';
ALTER ROLE super VALID UNTIL 'December 31, 2020';
postgres=# \du dvdrental;
How to tune PostgreSQL for memory

This article looks at parameters that can be used to help manage


memory in PostgreSQL. Recommended settings for each parameter
are also provided.

1. Shared_buffers (integer)

2. Work_mem (integer)

3. Maintenance_work_mem (integer)

4. Effective_cache_size (integer)

In this post, we are going to look at some of the important GUC


parameters recommended for memory management in
PostgreSQL, which is helpful for improving the performance of your
database server. All of these parameters reside under
the postgresql.conf file (inside $PDATA directory), which
manages the configurations of the database server.

shared_buffers (integer)
The shared_buffers parameter determines how much memory is
dedicated to the server for caching data.

The default value for this parameter, which is set


in postgresql.conf, is:

#shared_buffers = 128MB

The value should be set to 15% to 25% of the machine’s total RAM.
For example: if your machine’s RAM size is 32 GB, then the
recommended value for shared_buffers is 8 GB. Please note that
the database server needs to be restarted after this change.
work_mem (integer)
The work_mem parameter basically provides the amount of memory
to be used by internal sort operations and hash tables before writing
to temporary disk files. Sort operations are used for order by, distinct,
and merge join operations. Hash tables are used in hash joins and
hash based aggregation.

The default value for this parameter, which is set


in postgresql.conf, is:

#work_mem = 4MB

Setting the correct value of work_mem parameter can result in less


disk-swapping, and therefore far quicker queries.

We can use the formula below to calculate the


optimal work_mem value for the database server:

Total RAM * 0.25 / max_connections

The max_connections parameter is one of the GUC


parameters to specify the maximum number of concurrent
connections to the database server. By default it is set to 100
connections.

We can also directly assign work_mem to a role:

postgres=# alter user test set work_mem='4GB';

ALTER ROLE
maintenance_work_mem (integer)
The maintenance_work_mem parameter basically provides the
maximum amount of memory to be used by maintenance operations
like vacuum, create index, and alter table add foreign
key operations.

The default value for this parameter, which is set


in postgresql.conf, is:

#maintenance_work_mem = 64MB

It’s recommended to set this value higher than work_mem; this can
improve performance for vacuuming. In general it should be:
Total RAM * 0.05

effective_cache_size (integer)
The effective_cache_size parameter estimates how much
memory is available for disk caching by the operating system and
within the database itself. The PostgreSQL query planner decides
whether it’s fixed in RAM or not. Index scans are most likely to be
used against higher values; otherwise, sequential scans will be used if
the value is low. Recommendations are to
set Effective_cache_size at 50% of the machine’s total RAM.
PostgreSQL vs. Oracle: Difference in Costs, Ease of Use & Functionality

Functionality

PostgreSQL Oracle
PostgreSQL offers free scalability, and
Oracle Enterprise is recommended for high
Scalability can scale up to millions of transactions
workloads which are highly scalable, but costly.
per seconds.
Since the last few years, new
major PostgreSQL versions are released
every year and minor versions with bug
fixes are released every 3 months. One
New Oracle versions are generally available every
Updates of the best things about Postgres is that
2-4 years.
the PostgreSQL Global Development
Group announces the major and minor
release dates in advance for the
convenience of users and prospects.
PostgreSQL offers strong security
capabilities through different
authentication options (Host, LDAP,
PAM and certificates authentication)
Oracle offers advanced security packages, but as a
and role-based access control (user-
Security commercial database, they are available as
level, table-level and row-level). Data
an expensive add-on.
encryption can be achieved with
advanced security plugins
like pgcrypto which are available for
free.
Oracle supported master-slave and master-master
replication via Oracle Streams and Oracle multi-
PostgreSQL supports native streaming master in the older versions of Oracle Enterprise
Replication
replication and logical replication. Edition, which has now been replaced by Oracle
GoldenGate (a separately licensed application) for
all types of data replications.
PostgreSQL supports declarative Oracle supports general horizontal
Partitioning
partitioning. partitioning supported by all RDBMS.
Can only be deployed on Oracle Cloud and other
Can be deployed on any cloud provider,
Cloud popular cloud providers, but users must Bring
with a variety of PostgreSQL
Deployments Your Own License (BYOL) or use on-demand
hosting solutions available.
licensing.
Ease of Use

PostgreSQL Oracle
PostgreSQL PL/pgsql is compatible with
other relational databases like Oracle which Oracle infrastructure does not offer strong
Compatibility
makes it relatively easy to move to compatibility with open source RDBMS.
PostgreSQL.
PostgreSQL offers many free open-source
extensions, including:PostGIS - support for
geospatial objects store and
Oracle offers commercial add-ons that are
Extensions query. CitusDB - distributes data and
available for an additional license fee.
queries horizontally across
nodes. pg_repack - reorganizes tables
online to reclaim storage.
Oracle requires significantly more effort to
install and configure due to the hundreds of
PostgreSQL offers more light-weight tuning tuning variables and complex system
capabilities, like their Query Optimizer, and requirements. Most of the tuning capabilities
Tuning
DBaaS platforms like ScaleGrid offer provided from Automatic Workload Repository
advanced slow query analysis. (AWR) and database advisers comes bundled
with Oracle Enterprise Manager Database/Grid
control that requires the Enterprise Edition.
• FreeBSD
• AIX
• HP-UX
• HP-UX
• Linux
Supported • Linux
• NetBSD
Operating • OS X
• OpenBSD
Systems • Solaris
• OS X
• Windows
• SolarisUnix
• z/OS
• Windows
• C • .Net
• C# • C
• C++ • C++
• Clojure • Delphi
Supported • Cobol • Java
Languages • Delphi • JavaScript (Node.js)
• Eiffel • Perl
• Erlang • PHP
• Fortran • Python
• Groovy • Tcl
• Haskell
• Java
• JavaScript
• Lisp
• Objective C
• OCaml
• Perl
• PHP
• Python
• R
• Ruby
• Scala
• Tcl
• Visual Basic
PostgreSQL support is available free from
Oracle support for hardware and software
the community, and there are also many
Support packages is typically available at 22% of their
support providers available for advanced
licensing fees.
assistance.

Costs

PostgreSQL Oracle Standard Edition Oracle Enterprise Edition

Single Database Edition Costs $0 $17,500 $47,500


Clustering / Replication $0 Not available $23,000
Advanced Security $0 Not available $15,000
Software Update License &
$0 Not available $18,810
Support (annual)
Total Cost $0 $17,500 $104,310

The $47,500 licensing costs for Oracle Enterprise Edition is only for one CPU
core, that ultimately has to be multiplied with the actual number of cores on
the physical server. Oracle does offer discounts on their pricing, where you
can receive a 10% discount if you purchase online. There is also a wide
network of Oracle partners available to help you negotiate a discount, typically
ranging from 15%-30%, though larger discounts of up to 40%-60% are
available for larger accounts.

You might also like