×

Can I create a PostgreSQL database on web hosting?

The web hosting packages offered by IPHost do not provide the option to add PostgreSQL databases; we only support MySQL databases. So the simplest and shortest answer is - NO.

What is PostgreSQL?
PostgreSQL is an open-source database management system known for its performance, security, and extensibility. It is widely used for web applications and is compatible with multiple programming languages, such as PHP, Python, Java, and C++. For managing PostgreSQL, there are graphical interfaces like: phpPgAdmin – available in cPanel and similar to phpMyAdmin for MySQL, pgAdmin – a dedicated desktop/web app for managing PostgreSQL databases, and DBeaver – a cross-platform tool for working with databases.

If you use only a graphical tool, you can manage a PostgreSQL database without writing SQL code. However, for more advanced operations, it is recommended to know SQL.

Is PostgreSQL supported on web hosting?
On most shared web hosting packages, providers do not offer the ability to use PostgreSQL databases. One solution to use such a database would be to use a VPS package or a dedicated server, where you can manually install PostgreSQL, or purchase a web hosting package designed for PostgreSQL usage.

How do I manually install PostgreSQL on a VPS or dedicated server?
If you choose a VPS or dedicated server and want to manually install PostgreSQL, follow these steps (the following commands are intended for Ubuntu/Debian systems):

  1. Run the necessary system updates:
    sudo apt update
    sudo apt upgrade -y


  2. Install the necessary PostgreSQL packages:
    sudo apt install postgresql postgresql-contrib

Next, we will configure PostgreSQL:

  1. Set a password for the postgres user (the default admin account):
    sudo -u postgres psql
    ALTER USER postgres PASSWORD 'password';
    \q


  2. Edit the postgresql.conf file to accept external connections:
    sudo nano /etc/postgresql/14/main/postgresql.conf

  3. Find the line listen_addresses = 'localhost' and change it to:
    listen_addresses = 'ip_address'
    Instead of ip_address, enter the IP address or set of addresses you want to allow access to the database. You can use '*' to allow all connections.

  4. Edit the pg_hba.conf file to allow remote access:
    sudo nano /etc/postgresql/14/main/pg_hba.conf

  5. Replace the section # IPv4 local connections: with:
    # IPv4 local connections:
    host all all 0.0.0.0/0 md5


  6. Open port 5432:
    sudo ufw allow 5432/tcp

  7. Restart PostgreSQL:
    sudo service postgresql restart

You can also create a user for a PostgreSQL database:

  1. Create a database if you don’t already have one:
    sudo -u postgres createdb db_name

  2. Create a new user and grant access:
    sudo -u postgres createuser -P user_name
    sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE db_name TO user_name;"

How do I connect to a PostgreSQL database after installing it?
There are two ways to connect to a PostgreSQL database after installation:

  • From the command line.
  • Through external applications, like DBeaver or pgAdmin.

If you prefer connecting to the database from the command line, use the following line:

   psql -h server_ip -U user_name -d db_name -p 5432

The second method, through external applications, is much easier and recommended, as it uses a graphical interface. To connect to the PostgreSQL database, open the dedicated app and enter:

  • Host: The IP address of the server where the database is hosted.
  • Port: 5432 or another port designated for the connection.
  • User: The username with database access rights.
  • Password: The password set for this user.
  • Database: The name of the database to connect to.