By default, PostgreSQL uses port 5432. But for security’s sake, I want to change the default port to another one.
Problem
How to change the default port in PostgreSQL?
Solution
If you want to see the port used by PostgreSQL, you can see it in the postgresql.conf file by using the command (I’m using Ubuntu distro and PostgreSQL version 18):
Display the PostgreSQL port via postgresql.conf file
Or you can use the command below to see the PostgreSQL port:
sudo ss -ptuln | grep postgres
Display the PostgreSQL port via netstat
Or you can use the query in this post to see the port used by PostgreSQL:
SHOW PORT;
Display the PostgreSQL port via query
From the images above, you can see that PostgreSQL uses port 5432. If you want to change the PostgreSQL port to port 6543, for example, then go to the /etc/postgresql/18/main/postgresql.conf file if you use the Ubuntu distro and PostgreSQL version 18, and change the port value from 5432 to 6432.
Warning
If you’re using a distro other than Ubuntu/Debian, you can search for the postgresql.conf file by using the command:
sudo find / -type f -name "*postgresql.conf"
Then restart PostgreSQL using the command:
sudo systemctl restart postgresql
After that, you can check the PostgreSQL port by using one of the commands above, and the PostgreSQL port should have changed according to the port you want as shown in the image below:
Display the PostgreSQL port via netstat after changing the port
Note
If you have changed the default port of PostgreSQL from 5432 to 6432, for example, then you don’t need to write the port in the Linux command to access PostgreSQL if you access from localhost:
Access PostgreSQL from localhost after changing the default port
But, if you access PostgreSQL from another host, you have to write the option for the port, like in the picture below:
Access PostgreSQL from another host after changing the default port
Just as MariaDB requires the mariadb-client package to connect other hosts to a MariaDB database, PostgreSQL also requires the postgresql-client package to connect other hosts to a PostgreSQL database.
Problem
How to install postgresql-client on Linux?
Solution
Below is the command to install postgresql-client on some Linux distributions:
Then run the command below to see the installed PostgreSQL version:
psql --version
Check PostgreSQL version
As you can see in the image above, the version of postgresql-client that you installed is version 16.10. But you should know that, usually by default, the package provided by the distro is a stable old version and not the latest stable version. Therefore, if you want the mariadb-client package to use the latest stable version, use the command below if you use a Ubuntu/Debian distro:
How to Access a PostgreSQL Database From Another Host?
written by sysadmin | 6 December 2025
I want to access a PostgreSQL database from another host.
Problem
How to access a PostgreSQL database from another host?
Solution
I have 2 servers, the PostgreSQL server IP is 192.168.56.101, and the Ubuntu server IP is 192.168.56.11. I want to access the PostgreSQL database from the Ubuntu server. By default, use the format below if you want to access PostgreSQL:
psql -hip_db-uusername-ddb_name-pport_number
By default, the PostgreSQL database uses port 5432, so if you access your PostgreSQL using port 5432, you don’t need to write the port. I accessed my PostgreSQL by writing the command:
psql -h 192.168.56.101 -u postgres -d mydb
But I got an error like the image below:
psql: error: connection to server at ‘192.168.56.101’, port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?
Error when accessing the PostgreSQL database
The following are the steps to access a PostgreSQL database from another host:
1. Open port
Open port 5432 on each server. If you use RockyLinux/AlmaLinux/RHEL for your servers, use the command below:
Ownership of a database and the tables in it in PostgreSQL is very crucial because this has a big influence on users who want to carry out CRUD (Create, Read, Update, Delete) activities in a PostgreSQL database. If the user is not the owner of the database and the tables in it, then the user will not be able to perform CRUD.
Problem
How to change ownership in PostgreSQL?
Solution
For example, I have a mydb database and mydb_user as a user. When I access the database using the user mydb_user and perform a query:
select * from employee;
There is an error as shown below:
ERROR: permission denied for table employee
Error permission denied
After I searched on the internet, it turned out that this was caused by the user mydb_user not being the owner of the mydb database and the tables in it. To see the owner of the database, you can use the query below:
SELECT datname AS database_name,
pg_catalog.pg_get_userbyid(datdba) AS owner
FROM pg_database;
List the owners
You can see in the picture above, the postgres user is the owner of the mydb database. This is because when creating the database uses the postgres user and not the mydb_user.
INFO
You can also use an alternative query like the one below to see the owner of your database:
SELECT d.datname AS database_name, r.rolname AS owner
FROM pg_database d
JOIN pg_roles r ON d.datdba = r.oid
WHERE d.datname = 'your_database_name';
or using the simple query:
\l
Similarly, if you want to see the owners of the tables in the database (but you must first log in to that database to run this query), use the query below:
SELECT tablename, tableowner
FROM pg_tables
WHERE schemaname = 'public';
Then there will be a display like the following:
Show the owner of the tables
You can see in the image above that the owner of the table employee in the mydb database is a postgres user. This is because when creating the tables uses user postgres and user mydb.
INFO
You can also use a query like the one below to see the ownership of all tables in PostgreSQL:
SELECT
schemaname,
tablename,
tableowner
FROM pg_tables
ORDER BY schemaname, tablename;
A. Change database owner
If you want to change ownership of the mydb database from user postgres to user mydb_user, then you have to log in to PostgreSQL as an existing user, which in the case of a postgres user:
ALTER DATABASE mydb OWNER to mydb_user;
Run the query below to check ownership of the database:
\l
Change the database owner
You can see that the owner of the mydb database has changed to user mydb_user. But even so, you still can’t run CRUD on the database because the tables in the database still belong to the postgres user.
B. Change the owner of the table
Still using the postgres user, log in to the mydb database using the command:
\c mydb;
Then run the query below to change the table, sequence, and view to mydb_user:
DO $$
DECLARE
r RECORD;
BEGIN
-- Change the owner of all tables
FOR r IN SELECT schemaname, tablename FROM pg_tables WHERE schemaname = 'public'
LOOP
EXECUTE format('ALTER TABLE %I.%I OWNER TO mydb_user;', r.schemaname, r.tablename);
END LOOP;
-- Change the owner of all sequences
FOR r IN SELECT sequence_schema, sequence_name FROM information_schema.sequences WHERE sequence_schema = 'public'
LOOP
EXECUTE format('ALTER SEQUENCE %I.%I OWNER TO mydb_user;', r.sequence_schema, r.sequence_name);
END LOOP;
-- Change the owner of all views
FOR r IN SELECT table_schema, table_name FROM information_schema.views WHERE table_schema = 'public'
LOOP
EXECUTE format('ALTER VIEW %I.%I OWNER TO mydb_user;', r.table_schema, r.table_name);
END LOOP;
END $$;
Query to change the owner of the tables
After that, run the command below to view the ownership of the tables in the mydb database:
SELECT tablename, tableowner
FROM pg_tables
WHERE schemaname="public";
Change the owner of the tables
From the image above, you can see the owner of the tables in the mydb database is not a postgres user but a mydb_user. So, you should be able to do CRUD using the mydb_user user as shown in the image below:
Do CRUD on the database
Note
As a reminder, to create databases and tables in the database, you should not use postgres user but create a new user because it is very dangerous to use postgres user to carry out daily activities in the PostgreSQL database.
The previous article explained the basic commands in the PostgreSQL database. This article will explain how to set up a user in PostgreSQL.
Problem
How to manage a user in PostgreSQL?
Solution
By default, PostgreSQL runs MD5 encryption to save PostgreSQL user passwords in the pg_authid table. However, since PostgreSQL version 10 and later, PostgreSQL has added scram-sha-256 encryption, which is more secure compared to MD5 encryption. To use this encryption, you have to go to the /etc/postgresql/18/main/postgresql.conf file if you use PostgreSQL 18 and look for the word scram-sha-256 in the file, and open the fence located to the left of the word so it looks like the one below:
password_encryption = scram-sha-256
Then restart PostgresQL using the command:
sudo systemctl restart postgresql
A. List all users
To see all users in PostgreSQL, type the following command:
\du
List all users in PostgreSQL
By default, PostgreSQL uses the postgres user to access all databases in PostgreSQL.
B. Create a new user
To create a new user in PostgreSQL, use the format below on the PostgreSQL server:
For example, if you want to create a mydb_user user with the password qwerty, then use the command below:
create user mydb_user with encrypted password 'qwerty';
Create a user in PostgreSQL
C. Change user password
To change the password in PostgreSQL, use the format below:
ALTER USERusername WITH PASSWORD 'your_password';
For example, if you want to change the password for the mydb_user to q1w2e3, then use the command below:
ALTER USER mydb_user WITH PASSWORD 'q1w2e3';
Change the password in PostgreSQL
D. Create a user privilege
To give the user privileges for a database in PostgreSQL using the format below:
GRANTpermission_typeONdb_nameTOusername;
For example, in the previous article, you created a mydb database, so use the command below to grant all permission types:
grant all privileges on database mydb to mydb_user;
Grant all to the user in PostgreSQL
If you want a user to only be able to view the mydb database for the sysadmin user, use the command below:
CREATE user john with encrypted password '123456';
GRANT CONNECT ON DATABASE mydb TO john;
\c mydb
GRANT USAGE ON SCHEMA public TO john;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO john;
Grant viewer only in PostgreSQL
To see the grants you have made in PostgreSQL, use the command:
\l+
List all grants in PostgreSQL
E. Remove a user privilege
If you want to remove a privilege from a user, you can use the format below:
REVOKEpermission_typeONtable_nameFROMuser_name;
For example, if you want to remove a privilege for mydb_user, use the command below:
REVOKE all privileges on database mydb FROM mydb_user;
Revoke all grants from a user
Or, use the below command if you want to revoke from user john:
REVOKE CONNECT on database mydb FROM john;
Revoke connect only from a user
F. Delete a user
Use the format below to delete a user in PostgreSQL:
DROP USERusername;
If you want to delete mydb_user, use the command below:
DROP user mydb_user;
Error when dropping a user in PostgreSQL
You can see from the image above, there is an error when you want to delete a user. So, you have to remove some objects that depend on it. Use the command below:
REVOKE all privileges on database mydb FROM mydb_user;
and then delete the user using the below command:
DROP user mydb_user;
Drop a user in PostgreSQL
Note
If you want a user to only be able to access the PostgreSQL database for a certain time, for example, until January 31, 2030, use the command below:
How to Manage a Database and its Table(s) in PostgreSQL?
written by sysadmin | 6 December 2025
The previous article explained how to install a PostgreSQL database on Linux. This article will explain the basics of commands in a PostgreSQL database.
Problem
How to manage a database and its table(s) in PostgreSQL?
Solution
Below are the basic commands of PostgreSQL to manage a database and its table(s):
1. Access to the PostgreSQL database
Use the command below to access PostgreSQL:
sudo -u postgres psql
Access to the PostgreSQL
INFO
The use of capital letters in this article is only to distinguish between original commands from PostgreSQL and data from the user. You don’t have to use the capital letters when running these commands, but you can use all lowercase letters.
2. List all databases
Use the command below to list all databases in PostgreSQL:
\l
List all databases
3. Creating a new database
Use the format below to create a new database:
CREATE DATABASEdatabase_name;
You can see the options for this command on this page. For example, if you want to create a new database called mydb, use the command below;
CREATE DATABASE mydb;
Create a new database
4. Connect to the database
Use the format below to connect to the database:
\cdb_name;
For example, if you want to connect to the mydb database, type the following command:
You can see the options for this command on this page. Type the command below to create an employee table:
CREATE TABLE employee (name varchar (100), age int);
Create a table
6. Display all tables
Use the command below to display all the tables:
\dt
Display all table
7. Display the table structure
Use the format below to display the table structure:
\dtable_name;
For example, if you want to display the employee table structure, run the command below:
\d employee;
Describe a table
If you want to display more information about the table, type the command below:
\d+ employee;
Display more information in a table
8. Add the column
Use the format below to make a column in the table:
ALTER TABLEtable_nameADD columncolumn_nametype(nnn);
You can see the options for this command on this page. For example, if you want to add to the city column in the employee table, use the command below:
ALTER TABLE employee ADD column city varchar(100);
You can see the options for this command on this page. Type the command below if you want to insert 2 data to the employee table:
INSERT INTO employee (name,age,city) VALUES ('bob',21,'New York'), ('John',22,'Chicago');
Insert data
INFO
If you want to insert a value in the form of a number, the number does not have to be flanked with an apostrof (‘…’) sign, whereas if it is a character or a combination of characters and numbers, it must be flanked with an apostrof (‘…’).
10. Displays data in a table
Use the format below to display all data in a table:
SELECToption1FROMtable_name option2;
You can see the options for this command on this page. For example, use the command below to display all data in the employee table:
You can see the options for this command on this page. For example, if you want to update the age of the employee named Bob, use the command below:
UPDATE employee SET age=23 WHERE name='bob';
Update the data
12. Delete data
Use the format below to delete one or more rows of a table:
DELETE FROMtable_nameWHEREcolumn=value;
You can see the options for this command on this page. For example, if you want to delete the data where the user is in Chicago, use the command below:
DELETE FROM employee WHERE city='Chicago';
Delete the data
13. Delete the column
Use the format below to make changes in the table:
ALTER TABLEdb_nameDROP COLUMNcolumn_name;
If you want to delete the column, for example, city, you can use the following command:
ALTER TABLE employee DROP COLUMN city;
Delete the column
14. Empty the table
You can delete all data in a table with the format as below:
TRUNCATEtable_name;
You can see the options for this command on this page. For example, if you want to delete all the data in the employee table, type the command below:
TRUNCATE employee;
Truncate the table
15. Change a table name
You can change the name of the table using the format below:
ALTER TABLE old_table_name RENAME TO new_table_name;
You can see the options for this command on this page. Use the command below if, for example, you want to change the name of the employee table to employees:
ALTER TABLE employee RENAME TO employees;
Rename the table
16. Delete a table
Use the format below to delete a table:
DROP TABLEtable_name;
You can see the options for this command on this page. For example, if you want to delete the employee table, use the command below:
DROP TABLE employees;
Delete the table
17. Delete a database
To delete a database, use the format below:
DROP DATABASEdatabase_name;
You can see the options for this command on this page. For example, if you want to delete the mydb database, type the command below:
DROP DATABASE mydb;
Delete the database
17. Quit from the database
To quit from the database, run the command below:
\q
Quit from the database
Note
Actually, the basic commands for PostgreSQL and MariaDB are almost the same, with only there is a slight difference between the two databases.
PostgreSQL, also known as Postgres, is a free and open-source Relational Database Management System (RDBMS) emphasizing extensibility and SQL compliance.
Problem
How to install PostgreSQL on a Linux server?
Solution
In this article, I use the Linux distro RockyLinux server version 9.5, Ubuntu Server version 24.04, and OpenSUSE version 15.6. As of this writing (January 2025), the version of PostgreSQL that has been released is version 17.2.
A. Install PostgreSQL
Here is how to install PostgreSQL on some Linux distros:
If you want to install the latest version of PostgreSQL, go to this page and choose based on your Linux distro.
B. Connect to PostgreSQL
Now, connect to PostgreSQL using the command below:
sudo -u postgres psql
Access to the PostgreSQL
Note
By default, only Postgres users can enter the PostgreSQL server, so you run the command sudo -u postgres psql to connect to PostgreSQL. If you want to connect to PostgreSQL using other users, such as the root user, use the command below:
sudo -u root psql
There will be an error like below:
psql: error: connection to server on socket “/run/postgresql/.s.PGSQL.5432” failed: FATAL: role “root” does not exist
So, you have to define a new role for the root user. Connect to PostgreSQL, and run the command below:
CREATE ROLE root WITH SUPERUSER LOGIN;
CREATE DATABASE root;
\q
After that, try to connect to PostgreSQL using the root user; it should connect to PostgreSQL as shown in the image below: