Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
120 views

Introduction To Postgresql

PostgreSQL is a powerful, open source object-relational database system. It supports a wide range of data types and has tools for management and development. The files and programs include the PostgreSQL server, configuration files like postgresql.conf and pg_hba.conf, and client programs like psql. Installation involves downloading the installer, setting passwords, and allowing local connections.

Uploaded by

Tien Nguyen Manh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
120 views

Introduction To Postgresql

PostgreSQL is a powerful, open source object-relational database system. It supports a wide range of data types and has tools for management and development. The files and programs include the PostgreSQL server, configuration files like postgresql.conf and pg_hba.conf, and client programs like psql. Installation involves downloading the installer, setting passwords, and allowing local connections.

Uploaded by

Tien Nguyen Manh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

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

Free Commercial Database Limitations

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.

• Support for custom data types. PostgreSQL natively supports a large


number of data types by default, such as JSON, XML, H-Store, and others.
PostgreSQL takes advantage of it, being one of the few relational databases
with strong support for NoSQL features. Additionally, it allows users to define
their own data types.

• Easily-integrated third-party tools.

• Free, Open-source and community-driven. Postgres is completely open-


source and supported by its community, which strengthens it as a complete
ecosystem. Additionally, developers can always expect free and prompt
community assistance

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)

 The graphical installer for PostgreSQL includes


 the PostgreSQL server
 pgAdmin IV: a graphical tool for managing and
developing your databases
 and StackBuilder: a package manager that can be
used to download and install additional PostgreSQL
applications and drivers
10
Install and test
 Install
 Stop/start server (run as administrator)
 Connect to server from pgAdmin4

11
Install

12
Install

The directory contains


all data files

Must remember the port


number. By default, it is
5432

13 Must remember this password


Install

14
Install

You can uncheck on the


box and then Finish
because we don't need
any drivers at the
moment

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

You can stop/start


/restart the server
here

19
Connect to postgres from pgAdmin 4

20
Connect to postgres from pgAdmin 4

Right click here

21
Connect to postgres from pgAdmin 4

Enter the password of postgres account

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:

# Create the file repository configuration:


sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-
pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:


wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo
apt-key add -

# Update the package lists:


sudo apt-get update

# Install the latest version of PostgreSQL.


# If you want a specific version, use 'postgresql-12' or similar instead of
'postgresql':
sudo apt-get -y install postgresql
27 Source: https://www.postgresql.org/download/linux/ubuntu/
Install
 After installing PostgreSQLthe following packages are also
installed:
(Installed packages: postgresql, postgresql-client, postgresql-client-
common, postgresql-common, sysstat , …)

 If pgadmin4 (a graphical administration utility) is not installed,


use the apt-get command:
sudo apt-get install pgadmin4

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

– Connect to the server:


psql -h server.domain.org database user

Example: psql -h 192.168.100.5 postgres postgres


psql -h localhost postgres postgres

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

– Set a password for the "postgres" database role:


\password postgres and then give your password when prompted

– Exit the psql prompt: \q

 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

– To test your connection using psql: psql -U postgres -W

– To test your connection using pgAdmin, connect to the database at

localhost:5432 using the user name postgres and the password supplied

32
Stop/start/reload PostgreSQL server
– on Ubuntu

sudo service postgresql


{start|stop|restart|reload|force-reload|status}

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, …

 Default values = standard values

 If a configuration line is commented out, PostgreSQL uses


the default value for that entry
 Changing the default value = removing the comment symbol
from the line, reloading or restarting system
 Reverting to the default value for an entry = puting the
comment symbol back, stopping and restarting the
PostgreSQL system (NOT reloading)

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

 Most Unix administrators live and die by simple


command-line programs : psql
 Windows administrators will want to use the
graphical tools available in the pgAdmin
application. pgAdmin is also available on Ubuntu.

47
PostgreSQL Server Commands –
on Windows
 Location: C:\Program Files\PostgreSQL\x.x\bin
 postgres: the PostgreSQL database server

– The utility command pg_ctl can be used to start and


shut down the postgres server safely and comfortably

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}

 Can use pg_ctl


cd /usr/lib/postgresql/x.x/bin/
pg_ctl –D /var/lib/postgresql/x.x/main –l logfile start

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

pgAdmin III / pgAdmin IV: a fancy graphical


interface for administering a PostgreSQL system
– Open Source: www.pgadmin.org

53
54

You might also like