Cách cài đặt PostgreSQL trên Ubuntu 22.04

Article ID: 1074
Cập nhật gần nhất: 05 Th09, 2024

PostgreSQL là một hệ thống cơ sở dữ liệu quan hệ đối tượng mã nguồn mở mạnh mẽ với hơn 30 năm phát triển tích cực đã mang lại cho hệ thống này danh tiếng vững chắc về độ tin cậy, tính năng mạnh mẽ và hiệu suất.

Cài đặt PostgreSQL trên Ubuntu bằng cách sử dụng lệnh sau

apt update

apt install postgresql postgresql-contrib

Output:

root@hostvn:~# apt install postgresql postgresql-contrib
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages were automatically installed and are no longer required:
  galera-4 libconfig-inifiles-perl libdaxctl1 libdbd-mysql-perl libdbi-perl
  libmariadb3 libmysqlclient21 libndctl6 libpmem1 libsnappy1v5 liburing2
  mariadb-common socat
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm14 libpq5
  libsensors-config libsensors5 libtypes-serialiser-perl postgresql-14
  postgresql-client-14 postgresql-client-common postgresql-common sysstat

Vai trò và cơ sở dữ liệu PostgreSQL

Hostvn sẽ chuyển sang tài khoản người dùng Postgres cho các bước tiếp theo, để chuyển sang tài khoản Postgres, hãy sử dụng lệnh sau,

sudo -i -u postgres

Bạn có thể truy cập dấu nhắc PostgreSQL bằng tiện ích psql

psql

Output:

postgres@crown:~$ psql
psql (14.5 (Ubuntu 14.5-1ubuntu1))
Type "help" for help.

postgres=#

Để thoát khỏi shell postgres, hãy sử dụng lệnh bên dưới,

\q

Tạo vai trò PostgreSQL

Bây giờ chúng ta hãy xem làm thế nào để tạo thêm người dùng có thể tương tác với Cơ sở dữ liệu.

Để thực hiện điều này, bạn phải là người dùng postgres và sau đó chạy lệnh như hiển thị bên dưới,

createuser --interactive

Output:

postgres@vps:~$ createuser --interactive
Enter name of role to add: adam
Shall the new role be a superuser? (y/n) y

Tạo cơ sở dữ liệu PostgreSQL

Tạo cơ sở dữ liệu cực kỳ đơn giản. Chạy lệnh bên dưới với tư cách là tài khoản người dùng postgres.

createdb database_name

Ví dụ:

postgres@vps:~$ createdb my_db

Mở một dấu nhắc Postgres với vai trò mới

Để thực hiện điều này, chúng ta sẽ tạo một người dùng hệ thống Linux mới bằng cách sử dụng adduser.

Để đơn giản, chúng ta sẽ sử dụng cùng tên với tên mà chúng ta đã tạo vai trò postgres, adam .

Bạn sẽ cần phải chuyển lại thành người dùng root hoặc người dùng sudo có đủ quyền cần thiết.

adduser username

Ví dụ:

root@vps:~# adduser adam
Adding user `adam' ...
Adding new group `adam' (1000) ...
Adding new user `adam' (1000) with group `adam' ...
Creating home directory `/home/adam' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
...

Chuyển sang người dùng mới được thêm vào và kết nối với cơ sở dữ liệu

sudo -u username

Mở shell postgres và kết nối với Cơ sở dữ liệu mới,

psql -d database_name

Ví dụ:

root@vps:~# sudo -i -u adam

adam@vps:~$ psql -d my_db
psql (14.5 (Ubuntu 14.5-1ubuntu1))
Type "help" for help.

my_db=#

Sau khi đăng nhập với tên adam, hãy kiểm tra thông tin kết nối hiện tại của bạn:

\conninfo

Output:

my_db=# \conninfo
You are connected to database "my_db" as user "adam" via socket in "/var/run/postgresql" at port "5432".

Như vậy là kết thúc cài đặt PostgreSQL trên Ubuntu. Chúc các bạn thành công.

Article ID: 1074
Cập nhật gần nhất: 05 Th09, 2024
Lần sửa đổi: 1
Lượt xem: 0