Introduction To Postgresql
Introduction To Postgresql
1
Outline
1. Database environments
2. Comparing PostgreSQL
3. Installing PostgreSQL on Windows
4. The PostgreSQL Files and Programs
2
1. Database environments
3
A Simple Database engine
4
A multiuser PostgreSQL environment
5
2. Comparing PostgreSQL
6
PostgreSQL Versus Commercial
DBMS Products
PostGreSQL:
- free open source
- No limitations: CPU, Memory, Database
7
PostgreSQL
• Scalable. Vertical scalability is a hallmark of PostgreSQL. Considering that
almost any custom software solution tends to grow, resulting in database
extension, this particular option certainly supports business growth and
development.
8
3. Installing PostgreSQL on
Windows
9
Download
http://www.postgresql.org/download/windows
➔ Download the installer certified by EDB for all
supported PostgreSQL versions
(Lastest version 13)
11
Install
12
Install
14
Install
15
Check on start menu
16
Notes: Uninstall postgreSQL
Uninstall
Remove data directory
C:\Program Files\PostgreSQL
17
Server services
18
Server services
The server is running
19
Connect to postgres from pgAdmin 4
20
Connect to postgres from pgAdmin 4
21
Connect to postgres from pgAdmin 4
22
Connect to postgres from pgAdmin 4
23
Connect to postgres using psql
24
Connect to postgres using psql
25
4. Install PostgreSQL on Ubuntu
26
Install
Postgresql APT Repository:
28
Client installation
If you only wish to connect to an external PostgreSQL
server, install only the PostgreSQL client package:
– Client installation
sudo apt-get install postgresql-client
29
Basic Server Setup
Set the password of the PostgreSQL user (role) called
"postgres"
– Connect as a role with the same name as the local user (i.e. postgres)
to the database "postgres"
sudo -u postgres psql postgres
You can try to create the first database with the command
sudo -u postgres createdb mydb
30
Basic Server Setup
Allowing local connections
– By default, local connections are not allowed for the postgres user
– As a super user, open /etc/postgresql/x.x/main/pg_hba.conf (Ubuntu) in a text
editor:
sudo gedit /etc/postgresql/9.6/main/pg_hba.conf
– Scroll down to the line that describes local socket connections like this:
local all postgres peer
local all all peer
– Change the "peer" method to "md5"
– To allow connections using pgAdmin, find the line that describes local loopback
connections over IPv6:
host all all ::1/128 ident
– Change the "ident" method to "md5"
– Save and close the file
31
Basic Server Setup
Restart postgresql : sudo service postgresql restart
localhost:5432 using the user name postgres and the password supplied
32
Stop/start/reload PostgreSQL server
– on Ubuntu
33
Create superuser
Create a database superuser, same name as login name:
sudo -u postgres createuser --superuser $USER
sudo -u postgres psql
postgres=# \password $USER
34
5. The PostgreSQL Files
and Programs
35
PostgreSQL Files – on Windows
Default: C:\Program Files\PostgreSQL\x.x
36
Database cluster Directory
Default: C:\Program Files\PostgreSQL\x.x\data
37
PostgreSQL Files - on Ubuntu
Configuration files:
/etc/postgresql/x.x/main/
Data directory:
/var/lib/postgresql/x.x/main/
Log files:
/var/log/postgres/
38
pg_log directory for
Administrators
Each started time, a new log file: postgresl-year-
month-day-time.log
Each row: a timestamp + the event
39
PostgreSQL Log Message Levels
40
Configuration Files
How PostgreSQL behaves is controlled by three
separate configuration files
– postgresql.conf (C:\Program Files\PostgreSQL\x.x\data)
– pg_hba.conf
– pg_ident.conf
Text files:
– can be changed at any time
– will not take effect until either the system is restarted or
reloaded
– Each entry in the configuration files is on a separate line
– #: comment
41
Changing configuration files
Use text editor: notepad++, gedit, …
42
The postgresql.conf File
Format: featurename = value
Example: port = 5432
The main configuration file
– File Locations Section
– Connections and Authentication Section
– Resource Usage Section
– Write Ahead Log Section
– Query Tuning Section
– Error Reporting and Logging Section
– Runtime Statistics Section
– Autovacuum Parameters Section
– Client Connection Defaults Section
– Lock Management Section
– Version/Platform Compatibility Section
43
– Customized Options Section
The pg_hba.conf File
Configure:
– Which network hosts are allowed to connect to PostgreSQL
– Which PostgreSQL usernames can be used to connect from
the network
– What authentication method users must use to log into the
system
– Which PostgreSQL databases an authenticated client can
connect to
Format: connection-type database user network-
address login-method [options ]
Example: host all all 127.0.0.1/32 md5
44
The pg_hba.conf File
Format: connection-type database user network-address
login-method [options ]
Examples:
– host all all 127.0.0.1/32 md5 : allows any client on the
localhost to connect as any user to any database using
md5 authentication
– host all postgres 192.168.1.0/24 md5 : allows the
postgres user account to connect any database from the
local 192.168.1.0 subnetwork (192.168.1.0 to
192.168.1.255)
45
The pg_ident.conf File
Provides a method for you to map remote client
user accounts to PostgreSQL user accounts
Format: map-name ident-name PostgreSQL-user-account
Example:
– host all all 192.168.0.10/32 ident map=testhost: All users
from the host 192.168.0.10 will have access to all
PostgreSQL databases. User accounts from this host are
mapped to PostgreSQL user accounts using the testhost
ident mapping.
– testhost rich richard
– testhost mike michael
46 – testhost dan daniel
Programs
47
PostgreSQL Server Commands –
on Windows
Location: C:\Program Files\PostgreSQL\x.x\bin
postgres: the PostgreSQL database server
48
PostgreSQL Server Commands –
on Windows
pg_ctl: control the PostgreSQL system (stop,
start, or reload the configuration files, kill a
specified process)
– using the -D commandline option
– Example: C:\>pg_ctl stop -D "c:\ProgramFiles\PostgreSQL\x.x\data”
Document:
file:///C:/Program%20Files/PostgreSQL/x.x/doc/postgr
esql/html/app-pg-ctl.html
http://www.postgresql.org/docs/x.x/static/app-pg-
ctl.html
49
Stop/start/reload PostgreSQL server
– on Ubuntu
Easy way:
sudo service postgresql
{start|stop|restart|reload|force-reload|status}
50
PostgreSQL Client Applications
51
PostgreSQL Client Applications
psql: a command-line interface to the
PostgreSQL system
pg_config: see the current configuration values
used to compile and install the PostgreSQL
package
pg_dump: dump (or back up) the contents of a
database on the PostgreSQL system to a file
– Script: SQL files
– Archived: compressed binary files (using pg_restore to
restore)
52
PostgreSQL Client Applications
pg_dumpall: similar to the pg_dump program,
except it dumps all of the databases to a file
pg_restore: restore a PostgreSQL database from
an archive file created by pg_dump
53
54