0% found this document useful (0 votes)
18 views

CENG301 DBMS - Session-5

Uploaded by

grupsakli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CENG301 DBMS - Session-5

Uploaded by

grupsakli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CENG301 Database Management Systems

Session-5
Asst. Prof. Mustafa YENIAD
[email protected]
PostgreSQL GUI Tool pgAdmin by using docker
• Firstly, docker doemon have to be installed from the previous session.
• After that type the following:
$ docker run --name pgagmin4-web \
-p 5050:80 \
--restart always \
-e "[email protected]" \
DBMS

-e "PGADMIN_DEFAULT_PASSWORD=admin" \
-d dpage/pgadmin4
PostgreSQL settings: allow remote connections - Step-1
• Do not try the following steps on a live system unless you know what you are doing!
• Configuring PostgreSQL for remote connections is essential for accessing data from different
locations. By default, PostgreSQL only allows connections from the local machine, which can be a
limitation in many situations.
• Remote access to a PostgreSQL database is necessary for applications that require accessing the
data from different geographical locations or for teams that are working in different parts of the
world. It can also be crucial for troubleshooting issues that cannot be resolved locally or when the
DBMS

database administrator is working remotely.

• Edit the postgresql.conf file* (in different systems it is located in different place. I usually search
for it) you can find its location by typing:
$ sudo find / -name "postgresql.conf"

• Open postgresql.conf file, uncomment and edit this line: listen_addresses = 'localhost'
• to: listen_addresses = '*'

• or: listen_addresses = '0.0.0.0'


* to backup postgresql.conf configuration file is strongly recommended!
PostgreSQL settings: allow remote connections - Step-2
• Apply authentication configuration by editing pg_hba.conf file *.
• pg_hba.conf is normally located in the same directly as postgresql.conf. If not, you can search it.
• Open that file with text-editor (i.e. vi) and add the following two lines at the end of the file:
host all all 0.0.0.0/0 md5
host all all ::/0 md5

• Important information:
DBMS

• Authentication methods (also new additonal methods are available upon the postgres versions):
• trust: the least secure authentication, essentially no password is needed!
• md5: very common, requires an md5-encrypted password to connect.
• ident: Uses pg_ident.conf file to check whether the OS account of the user trying to connect has a mapping
to a PostgreSQL account. The password is not checked. ident is not available on Windows.
• peer: Uses the OS name of the user from the kernel. It is available only for Linux, FreeBSD, macOS, and
Solaris, and only for local connections on these systems.
• cert: The client must have a registered certificate. cert uses an ident file such as pg_ident to map the
certificate to a PostgreSQL user.

• Now, restart the postgres service:


$ sudo systemctl restart postgresql.service
* to backup pg_hba.conf configuration file is strongly recommended!
PostgreSQL settings: allow remote connections - Step-3
• Enable the PostgreSQL port for external connections from the firewall (for RedHat distros):
$ sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent
$ sudo firewall-cmd --reload

• For Debian distros (like Ubuntu or Mint):


$ sudo ufw allow 5432/tcp
DBMS

• Connect to PostgreSQL database server and change the password of the postgres user:
$ sudo --login -u postgres

• Login to postgres shell and set a new password for postgres user:
postgres@[hostname]:~ $ psql

postgres# ALTER USER postgres with password 'new_password';


OR
postgres# ALTER USER postgres with encrypted password 'new_password';
PostgreSQL settings: allow remote connections - Step-4
• Now, go to your browser window and open the following URL:
DBMS

• After opening the session, register a new Server (your host) and test a new Connection.
PostgreSQL Important Actions (create a new db, user and set permissions)
• Now, create a new database named dvdental:
postgres=# CREATE DATABASE dvdrental;
• Create a new user named myuser: (or any username you desire)
postgres=# CREATE USER myuser PASSWORD 'new_password';
• Set all permissions to myuser for dvdrental database:
DBMS

postgres=# GRANT ALL PRIVILEGES ON DATABASE dvdrental TO myuser;

• List all users:


postgres=# SELECT USENAME FROM pg_user;

• Go back to Linux shell:


postgres=# \q
postgres@[hostname]:~ $ logout

• All these steps can also be applied easily by using pgAdmin tool.
PostgreSQL Important Actions (import / restore a db dump file i.e. from backup)
• Using shell is always recommended when import (especially huge) db-dump files.
• Firstly, download dump (dvdrental.zip) file provided in Aybuzem.
• Then, move (mv) or copy (cp) it to postgres user's home directory (/var/lib/pgsql).
$ sudo cp ~/Downloads/dvdrental.zip /var/lib/pgsql/
• Connect to postgres user environment, extract the archive file, list the directory contents and go to db shell
: --login -u postgres
$ sudo
DBMS

postgres@[hostname]:~ $ unzip dvdrental.zip


postgres@[hostname]:~ $ ls -la

• Finally import the db tape archive .tar (dvdental.tar) into PostgreSQL service:
postgres@[hostname]:~ $ pg_restore -U postgres -d dvdrental dvdrental.tar
PostgreSQL Important Actions
• Open the postgres shell and check the status:
postgres@[hostname]:~ $ psql
• Show / list all databases:

postgres=# \l
postgres=# \l+
DBMS

postgres=# \list

• Connect to dvdrental database:


postgres=# \connect dvdrental;
• Show tables:
postgres=# \dt;

• Go back to Linux shell:


postgres=# \q
postgres@[hostname]:~ $ logout

You might also like