0% found this document useful (0 votes)
3 views27 pages

1.Introduction2PostgreSQL

Uploaded by

Xuân Cường
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views27 pages

1.Introduction2PostgreSQL

Uploaded by

Xuân Cường
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

28/02/2024

Introduction to PostgreSQL

Outline
1. Database environments
2. Comparing PostgreSQL
3. Installing PostgreSQL on Windows
4. The PostgreSQL Files and Programs

1
28/02/2024

1. Database environments

A Simple Database engine

2
28/02/2024

A multiuser PostgreSQL environment

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

3
28/02/2024

3. Installing PostgreSQL on
Windows

Download
http://www.postgresql.org/download/windows
è Download the installer certified by EDB for all
supported PostgreSQL versions
(Lastest version 16)

— 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

10

4
28/02/2024

Install and test


— Install
— Stop/start server (run as administrator)
— Connect to server from pgAdmin4

11

11

Install

12

12

5
28/02/2024

Install

The directory contains


all data files

Must remember the port


number. By default, it is
5432

13 Must remember this password

13

Install

14

14

6
28/02/2024

Install

You can uncheck on the


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

15

15

Check on start menu

16

16

7
28/02/2024

Notes: Uninstall postgreSQL

— Uninstall
— Remove data directory
C:\Program Files\PostgreSQL

17

17

Server services

18

18

8
28/02/2024

Server services
The server is running

You can stop/start


/restart the server
here

19

19

Connect to postgres from pgAdmin 4

20

20

9
28/02/2024

Connect to postgres from pgAdmin 4

Right click here

21

21

Connect to postgres from pgAdmin 4

Enter the password of postgres account

22

22

10
28/02/2024

Connect to postgres from pgAdmin 4

23

23

Connect to postgres using psql

24

24

11
28/02/2024

Connect to postgres using psql

25

25

4. Install PostgreSQL on Ubuntu

26

26

12
28/02/2024

Install
Postgresql APT Repository:
(https://www.postgresql.org/download/linux/ubuntu/)

# 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

27

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

28

13
28/02/2024

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

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

30

14
28/02/2024

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

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

32

15
28/02/2024

Stop/start/reload PostgreSQL server


– on Ubuntu

sudo service postgresql


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

33

33

Create superuser
— Create a database superuser, same name as login name:
# Create a new superuser
sudo -u postgres createuser --superuser $USER

# Set password for the new user


sudo -u postgres psql
postgres=# \password $USER

34

34

16
28/02/2024

5. The PostgreSQL Files


and Programs

35

35

PostgreSQL Files – on Windows


— Default: C:\Program Files\PostgreSQL\x.x

36

36

17
28/02/2024

Database cluster Directory


— Default: C:\Program Files\PostgreSQL\x.x\data

37

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

38

18
28/02/2024

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

39

PostgreSQL Log Message Levels

40

40

19
28/02/2024

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

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

42

20
28/02/2024

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

43

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

44

21
28/02/2024

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

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

46

22
28/02/2024

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

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

48

23
28/02/2024

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

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

50

24
28/02/2024

PostgreSQL Client Applications

51

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

52

25
28/02/2024

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

53

6. Practice on pgAdmin 4

54

54

26
28/02/2024

Minidb
— Create a mini database: minidb

— 2 tables:
– Student(ID, firstname, lastname, dob, gender, class_id)
– Class(classid, name, managername)

• Create all primary keys and foreign key

55

55

56

56

27

You might also like