PostgreSQL Tutorial
PostgreSQL Tutorial
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.
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
3) Geospatial database
Language support
PostgreSQL support most popular programming languages:
• Python
• Java
• C#
• C/C+
• Ruby
• JavaScript (Node.js)
• Perl
• Go
• Tcl
• 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
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.
PostgreSQL Instance
# Install PostgreSQL:
sudo yum install -y postgresql14-server
cat /etc/yum.repos.d/pgadmin4.repo
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"
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
Tablespace postgres
pg_default : store user Data
Pg_global : store system Data
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
1. Shared_buffers (integer)
2. Work_mem (integer)
3. Maintenance_work_mem (integer)
4. Effective_cache_size (integer)
shared_buffers (integer)
The shared_buffers parameter determines how much memory is
dedicated to the server for caching data.
#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.
#work_mem = 4MB
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.
#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
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.