📜  postgres setup Linux Debian - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:03:48.834000             🧑  作者: Mango

Postgres Setup Linux Debian - Shell-Bash

PostgreSQL is a free, open-source relational database management system. In this guide, we will walk through the steps to install and configure PostgreSQL on a Linux Debian server using the Shell-Bash environment.

Prerequisites
  • A Linux Debian server
  • The Shell-Bash environment
  • Sudo or root access to the server
Step 1: Install PostgreSQL

The first step is to install PostgreSQL on the server. This can be done using the following command in the Shell-Bash environment:

sudo apt-get update
sudo apt-get install postgresql

Step 2: Configure PostgreSQL

Once PostgreSQL is installed, we need to configure it. By default, PostgreSQL is configured to only accept local connections. We need to change this to accept connections from any IP address.

To do this, open the PostgreSQL configuration file with the following command:

sudo nano /etc/postgresql/<version>/main/postgresql.conf

Replace <version> with the version of PostgreSQL installed on your server. For example, if you installed PostgreSQL version 12, the path would be /etc/postgresql/12/main/postgresql.conf.

Find the line that reads #listen_addresses = 'localhost'. Uncomment the line by removing the # character, and change 'localhost' to '*'. The line should now read: listen_addresses = '*'.

Next, we need to allow remote access to the server. To do this, we need to edit the pg_hba.conf file. Open the file with the following command:

sudo nano /etc/postgresql/<version>/main/pg_hba.conf

Again, replace <version> with the version of PostgreSQL installed on your server.

Add the following lines to the bottom of the file:

host    all             all             <ip_address>/32       md5

Replace <ip_address> with the IP address of the remote server you want to allow access to. If you want to allow access from any IP address, replace <ip_address> with 0.0.0.0.

Save and close the file.

Step 3: Restart PostgreSQL

To apply the changes we made in the previous steps, we need to restart the PostgreSQL service. We can do this with the following command:

sudo service postgresql restart

Step 4: Connect to PostgreSQL

Now that PostgreSQL is installed and configured, we can connect to it using the command line.

To connect to PostgreSQL, we need to use the psql command. You can connect by running the following command:

psql -h <server_ip> -U postgres

Replace <server_ip> with the IP address of the server running PostgreSQL.

Conclusion

That's it! You have successfully installed and configured PostgreSQL on a Linux Debian server using the Shell-Bash environment. You should now be able to connect to PostgreSQL and start working with your databases.