sudo apt install postgresql -y<\/pre>\nOnce installed, start and enable the PostgreSQL service:<\/p>\n
systemctl start postgresql.service && systemctl enable postgresql.service<\/pre>\nTo check the status of the service:<\/p>\n
systemctl status postgresql.service<\/pre>\nIf everything is ok, you should get the following output:<\/p>\n
root@host:~# systemctl status postgresql.service\r\n\u25cf postgresql.service - PostgreSQL RDBMS\r\n Loaded: loaded (\/lib\/systemd\/system\/postgresql.service; enabled; vendor preset: enabled)\r\n Active: active (exited) since Tue 2022-10-18 17:22:36 CDT; 38s ago\r\n Main PID: 307278 (code=exited, status=0\/SUCCESS)\r\n CPU: 3ms\r\n\r\nOct 18 17:22:36 host.test.vps systemd[1]: Starting PostgreSQL RDBMS...\r\nOct 18 17:22:36 host.test.vps systemd[1]: Finished PostgreSQL RDBMS.\r\n<\/pre>\nNow, when the PostgreSQL database service is installed, we can start with the PostgreSQL<\/b> commands.<\/p>\n<\/span>1. Log in to PostgreSQL<\/span><\/h2>\nTo log in to the PostgreSQL admin command line, execute the following command:<\/p>\n
sudo -u postgres psql<\/pre>\nOnce logged in, you should receive the following output:<\/p>\n
root@host:~# sudo -u postgres psql\r\ncould not change directory to \"\/root\": Permission denied\r\npsql (14.5 (Ubuntu 14.5-0ubuntu0.22.04.1))\r\nType \"help\" for help.\r\n\r\npostgres=#<\/pre>\n<\/span>2. List databases<\/span><\/h2>\nTo list the database in Postgres, execute the following command:<\/p>\n
\\l<\/pre>\nIt is a simple command \\l<\/b> which will retrieve all databases in a table like this:<\/p>\npostgres=# \\l\r\n List of databases\r\n Name | Owner | Encoding | Collate | Ctype | Access privileges\r\n-----------+----------+----------+-------------+-------------+-----------------------\r\n postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |\r\n template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c\/postgres +\r\n | | | | | postgres=CTc\/postgres\r\n template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c\/postgres +\r\n | | | | | postgres=CTc\/postgres\r\n(3 rows)\r\n<\/pre>\n<\/span>3. Get the size of the Database<\/span><\/h2>\nTo get the size of the postgres<\/b> database, you need to connect to it first. To do that, execute the following command:<\/p>\n\\c postgres<\/pre>\nOnce connected, you should receive the following output:<\/p>\n
postgres=# \\c postgres\r\nYou are now connected to database \"postgres\" as user \"postgres\".<\/pre>\nNow, we can check the size of the current (postgres) database<\/p>\n
SELECT pg_database_size(current_database());<\/pre>\nThis will return the size in bytes<\/p>\n
postgres=# SELECT pg_database_size(current_database());\r\n pg_database_size\r\n------------------\r\n 8774435\r\n(1 row)<\/pre>\nIf you want the output to be in the human-readable format in Kilobytes, Megabytes, or Gigabytes execute the following command:<\/p>\n
select pg_size_pretty(pg_database_size(current_database()));<\/pre>\nNow, the output should look like this:<\/p>\n
pg_size_pretty\r\n----------------\r\n 8569 kB\r\n(1 row)<\/pre>\n<\/span>4. List Users and assigned Roles<\/span><\/h2>\nTo list the users and their assigned roles, execute the following command:<\/p>\n
\\du<\/pre>\nYou should receive the table as output with the PostgreSQL user and its privileges as described below:<\/p>\n
postgres=# \\du\r\n List of roles\r\n Role name | Attributes | Member of\r\n-----------+------------------------------------------------------------+-----------\r\n postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}\r\n<\/pre>\n<\/span>5. Creating Database and User in PostgreSQL<\/span><\/h2>\nTo create a new database in PostgreSQL, execute the following command:<\/p>\n
create database testdb;<\/pre>\nOnce the database is created, you will receive the following output:<\/p>\n
postgres=# create database testdb;\r\nCREATE DATABASE\r\n<\/pre>\nTo create a new user with a password, execute the following command:<\/p>\n
CREATE USER testuser WITH ENCRYPTED PASSWORD 'StrongPasswordHere';<\/pre>\nOnce the user is created, you will receive the following output:<\/p>\n
postgres=# CREATE USER rosehosting WITH ENCRYPTED PASSWORD 'StrongPasswordHere';\r\nCREATE ROLE<\/pre>\nNow, let’s add privileges on the created database to the newly created user.<\/p>\n
grant all privileges on database testdb to testuser;<\/pre>\nNow, if you list the databases with the \\l<\/b> command, you will receive the following lines:<\/p>\n Name | Owner | Encoding | Collate | Ctype | Access privileges\r\n-----------+----------+----------+-------------+-------------+----------------------\r\ntestdb<\/b>| postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc\/postgres +\r\n | | | | | postgres=CTc\/postgres+\r\n | | | | | testuser<\/b>=CTc\/postgres\r\n<\/pre>\n