Otu esi etinye ma hazie PostgreSQL na Ubuntu 20.04

PostgreSQL, nke a na-akpọkarị Postgres, bụ usoro nchekwa data dị ike, mepere emepe ihe na njikọ. PostgreSQL ewulitela aha siri ike maka ntụkwasị obi, njirimara siri ike na arụmọrụ n'etiti ndị mmepe.

Ị ga -ama otu esi etinye PostgreSQL na Ubuntu 20.04 LTS Focal Fossa gị Sistemụ arụmọrụ na ntuziaka na-esonụ. Otu ụkpụrụ ahụ ga-arụ ọrụ maka ụdị ọhụrụ Ubuntu 21.04 (Hirsute Hippo).

Prerequisites

Lelee ma melite sistemụ arụmọrụ Ubuntu 20.04 gị.

sudo apt update && sudo apt upgrade -y

Advertisement


Wụnye PostgreSQL na Ubuntu 20.04

Site na ndabara, PostgreSQL dị na ebe nchekwa Ubuntu. Ị nwere ike ịwụnye ya site na isi iyi, nke dị n'ihu ebe nchekwa ndabere.

Wụnye PostgreSQL site na iji apt

Iji tinye PostgreSQL site na iji njikwa ngwugwu dabara na Ubuntu 20.04, tinye iwu a:

sudo apt install postgresql-client

Ị ga-etinye ya (Ngwungwu enyemaka PostgreSQL), nke nwere mgbakwunye maka sistemụ nchekwa data PostgreSQL.

Ihe atụ mmepụta:

~$ sudo apt install postgresql postgresql-contrib
 [sudo] password for bytesboss: 
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following additional packages will be installed:
   libllvm10 libpq5 postgresql-12 postgresql-client-12 postgresql-client-common
   postgresql-common sysstat
 Suggested packages:
   postgresql-doc postgresql-doc-12 libjson-perl isag
 The following NEW packages will be installed:
   libllvm10 libpq5 postgresql postgresql-12 postgresql-client-12
   postgresql-client-common postgresql-common postgresql-contrib sysstat
 0 upgraded, 9 newly installed, 0 to remove and 3 not upgraded.
 Need to get 30.6 MB of archives.
 After this operation, 121 MB of additional disk space will be used.
 Do you want to continue? [Y/n]

Tinye "Y" ka ịga n'ihu na ntinye. Usoro kwesịrị iwe ihe na-erughị 20 sekọnd. Ozugbo etinyere ya, ị nwere ike nyochaa ntinye PostgreSQL site na ịpị iwu a:

sudo systemctl status postgresql

Ihe atụ mmepụta:

~$ sudo systemctl status postgresql
 ● postgresql.service - PostgreSQL RDBMS
      Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
      Active: active (exited) since Tue 2021-06-29 05:00:05 PDT; 3min 29s ago
    Main PID: 4238 (code=exited, status=0/SUCCESS)
       Tasks: 0 (limit: 4617)
      Memory: 0B
      CGroup: /system.slice/postgresql.service
 Jun 29 05:00:05 ubuntu systemd[1]: Starting PostgreSQL RDBMS…
 Jun 29 05:00:05 ubuntu systemd[1]: Finished PostgreSQL RDBMS.

Ọ bụrụ na akwadoghị ya site na ohere ụfọdụ, pịnye naanị mmalite wee mee site na iwu buut dị n'okpuru.

sudo systemctl enable postgresql && sudo systemctl start postgresql

Wụnye PostgreSQL site na isi mmalite

Iji wụnye PostgreSQL site na isi mmalite, ị ga-achọ itinye mgbakwunye ụfọdụ. Nke mbụ, tinye iwu na-esonụ iji mee ka sistemụ Ubuntu gị dị ọhụrụ.

sudo apt install wget curl ca-certificates 

Na-esote, budata igodo GPG achọrọ maka ịgbakwunye ebe nchekwa isi mmalite PostgreSQL:

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

Ugbu a tinye mepụta PPA omenala dị ka ndị a. Rịba ama na nke a bụ naanị maka Ubuntu 20.04. Ị ga-achọ ịgbanwe akụkụ isi ka ị nweta ọrụ ma ọ bụrụ na ị na-eji Ubuntu 21.04.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ \
 focal-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' 

Gaa n'ihu imelite ebe nchekwa gị wee wụnye PostgreSQL na PostgreSQL-Contrib

sudo apt update && sudo apt-get install postgresql postgresql-contrib -y

Ihe atụ mmepụta:

~$ sudo apt-get install postgresql postgresql-contrib 
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following additional packages will be installed:
   libpq5 postgresql-13 postgresql-client-13
 Suggested packages:
   postgresql-doc postgresql-doc-13
 The following NEW packages will be installed:
   postgresql postgresql-13 postgresql-client-13 postgresql-contrib
 The following packages will be upgraded:
   libpq5
 1 upgraded, 4 newly installed, 0 to remove and 7 not upgraded.
 Need to get 16.9 MB of archives.
 After this operation, 56.0 MB of additional disk space will be used.
 Do you want to continue? [Y/n] y

Tinye "Y" wee gaa n'ihu na nrụnye. Ozugbo emechara, nyochaa ọkwa nrụnye na ọkwa ọrụ.

sudo systemctl status postgresql

Ọ bụrụ na arụnyere ya nke ọma, ị ga-enweta ọkwa dị mma, dịka ọ dị na ọmụmaatụ gara aga na-eji njikwa dabara adaba.

Iwu ọrụ maka PostgreSQL

Ihe nkesa nchekwa data PostgreSQL na-arụ dị ka ọrụ n'okpuru aha "postgresql". Ị nwere ike ijikwa ọrụ ahụ site n'ịgba iwu ndị dị n'okpuru.

  • Kwụsị ihe nkesa PostgreSQL:
sudo systemctl stop postgresql
  • Malite ihe nkesa PostgreSQL:
sudo systemctl start postgresql
  • Malitegharịa PostgreSQL (dịka ọmụmaatụ mgbe agbanwechara ntọala nhazi) ihe nkesa:
sudo systemctl restart postgresql
  • Bugharịa ihe nkesa PostgreSQL:
sudo systemctl reload postgresql
  • Lelee ọkwa PostgreSQL:
sudo systemctl status postgresql

Ekele, ị tinyela ihe nkesa nchekwa data PostgreSQL nke ọma.


Advertisement


Hazie ihe nkesa PostgreSQL

Ịgbanwe na akaụntụ Postgres

Tupu ịga n'ihu, rịba ama, ọ bụ naanị ndị isi na ndị ọrụ nwere oke ọrụ nwere ike ịmepụta ọrụ ọhụrụ na Postgres. N'oge nrụnye, e mepụtara akaụntụ onye ọrụ a na-akpọ Postgres jikọtara ya na ọrụ Postgres ndabara.

Iji tinye aka na akaụntụ ahụ, ị ​​nwere ike ịme iwu ndị a:

sudo -i -u postgres

Ugbu a na ịbanye n'elu, ị nwere ike ịnweta PostgreSQL ozugbo site na ịpị:

psql

Ị ga-achọpụta ozugbo na ọnụ ahịa agbanweela na (postgres=#) ugbu a nọ n'ahịrị iwu ọnụ. Nke a pụtara na ị jikọọla na nchekwa data nke ọma.

Iji pụọ na nchekwa data Postgres, ị nwere ike ime nke a site na ịpị ihe ndị a:

\q

Nhọrọ maka ịgbanwee akaụntụ Postgres

Ụzọ ọzọ ị ga-esi na-emekọrịta ihe na nchekwa data Postgres na-agbanweghị akaụntụ onye ọrụ bụ iji iwu sudo jikọọ ozugbo. Ị nwere ike ime nke a site na ịpị:

sudo -u postgres psql

Nke a dị mma iji karịa, n'ihi na ọ na-echekwa oge ịdebanye aha gị ozugbo na-enweghị ịmekwu iwu bash ọnụ.

Iji pụọ na njedebe, dịka nhọrọ nke mbụ, ị nwere ike ịpụ site na ịpị ihe ndị a:

/q

Mepụta onye ọrụ & database

Ugbu a ị nwere ike ịmepụta ọrụ onye ọrụ ma ọ bụrụ na-ịchọrọ. Mara, naanị superusers na ọrụ nwere (createrole) ihe ùgwù nwere ike ịmepụta ọrụ ọhụrụ. Iji mepụta onye ọrụ ọhụrụ, pịnye ihe ndị a:

sudo su - postgres -c "createuser <name>"

Ugbu a ị nwere ike ịmepụta nchekwa data PostgreSQL maka onye ọrụ ọhụrụ ị mepụtara:

sudo su - postgres -c "createdb <namedb>"

Iji mechaa, ị ga-agbanye na akaụntụ superuser Postgres iji nye ikike na nchekwa data ọhụrụ.

Jikọọ na akaụntụ onye ọrụ Postgres super:

sudo -u postgres psql

Nye ohere ịnweta ka :

GRANT ALL PRIVILEGES ON DATABASE <usernamedb> TO <name>;

Ozugbo emechara, ka ịpụ, pịnye ihe ndị a:

/q

UFW Firewall nyere maka PostgreSQL

Ị ga-achọ ịmepụta iwu ọkụ ọkụ UFW iji kwe ka PostgreSQL nwee mkparịta ụka na ọdụ ụgbọ mmiri ya 5432 na achọrọ ma ọ bụrụ na ị na-eme atụmatụ iji ihe ọ bụla ọzọ ma e wezụga localhost. Enwere ike ime nke a site na ịpị iwu a:

sudo ufw allow proto tcp from 192.168.1.0/24 to any port 5432

Advertisement


Nweta tere aka na PostgreSQL

Ọ bụrụ na ịchọrọ ịnweta ndabara na PostgreSQL, nke a nwere ike ime ngwa ngwa site na ịgbanwe ndabara na-ege ntị na interface mpaghara nke (127.0.0.1) na IP, subnet ma ọ bụ ọbụna interfaces niile, nke enwere ike ime na (postgresql.conf) faịlụ.

Mbụ, mepee faịlụ postgresql.conf site na iji nano:

sudo nano /etc/postgresql/12/main/postgresql.conf

Ugbu a gbadaa ala ruo mgbe ịchọtara ya (Ntọala njikọ) ma gbanwee ( listen_addresses = 'localhost') ka adreesị. Ọtụtụ ga-ahọrọ ige ntị n'akụkụ niile, ọkachasị mgbe ọ na-agba ọsọ n'ọtụtụ sava chọrọ ijikọ na nchekwa data PostgreSQL site na ịgbanwe. localhost na (*).

Ihe Nlereanya:

adreesị postgress gee ubuntu 20.04

Ugbu a chekwaa faịlụ ahụ (CTRL+O), wee pụọ (CTRL+X) wee malitegharịa ihe atụ PostgreSQL gị:

sudo service postgresql restart

Iji lelee na mgbanwe ndị ị mere dị ndụ, ndị ọrụ nwere ike iji ss utility arụnyere n'ime Ubuntu.

ss -nlt | grep 5432

Ị ga-ahụ ihe ndị a:

na-ege ntị lelee postgresql na ubuntu

Iji mechaa, ị nwere ike hazie ihe nkesa ka ọ nabata njikọ dịpụrụ adịpụ site na-edezi faịlụ (pg_hba.conf).

Mepee (pg_hba.conf) site na iji nano:

sudo nano /etc/postgresql/12/main/pg_hba.conf

Site na ebe a, ị nwere ike ịtọ nhọrọ dị iche iche. Ụfọdụ ọmụmaatụ n'okpuru:

ihe atụ postgresql

Okwu na mmechi:

N'ime ntuziaka ahụ, ị ​​mụtala otu esi etinye PostgreSQL site na iji njikwa ngwugwu dabara adaba na site na isi iyi, nke akwadoro karịa ma ọ bụrụ na ị na-emecha atụmatụ ọhụrụ na ịmepụta ọrụ na nchekwa data nwere njikọ dịpụrụ adịpụ. N'ozuzu, PostgreSQL nwere ọtụtụ uru n'ihe gbasara ndakọrịta, scalability, nche, na atụmatụ ndị ọzọ tụnyere usoro njikwa nchekwa data ndị ọzọ. N'adịghị ka ndị ọzọ Sistemụ Njikwa nchekwa data (DBMS), ọ na-akwado ya site na nnukwu netwọk nke ụlọ ọrụ na-emepụta obodo dị n'otu siri ike.

Maka ozi ndị ọzọ maka iji PostgreSQL, gaa na onye ọrụ gọọmentị akwụkwọ.

Ọ bụrụ na ị nwere ajụjụ, nweere onwe gị ịhapụ ikwu okwu n'okpuru.

Ahapụ a Comment