melx's avatar
Level 4

Connect postgresql from another server Redhat 8(Linux )

I have two servers one is for web application and another is for database,

I have install the laravel app in the first server(web server) and pg database installed in the database server,

Kindly how can i connect it my database from the second server in .ev file, or what is the logic to connect it,

      DB_CONNECTION=pgsql
      DB_HOST=127.0.0.1
      DB_PORT=5432
      DB_DATABASE=db_test
      DB_USERNAME=postgres
      DB_PASSWORD=1234567
0 likes
2 replies
LaryAI's avatar
Level 58

You can connect to the Postgres database from the web server by setting the DB_HOST to the IP address of the database server. You will also need to make sure that the Postgres server is configured to accept connections from the web server.

To do this, you will need to edit the pg_hba.conf file on the database server. This file is usually located in the data directory of the Postgres installation.

Add the following line to the pg_hba.conf file:

host    all             all             <web_server_ip_address>/32            md5

This will allow the web server to connect to the Postgres server using the username and password specified in the .env file.

Once you have done this, restart the Postgres server and you should be able to connect to the database from the web server.

melx's avatar
Level 4

@LaryAI thanks for your quickly response, i will follow up this instruction accordingly,

but how about DB_PORT i can left this PORT=5432, or if i have my port i can set it ?

Please or to participate in this conversation.