How to install and configure PostgreSQL database on Linux?

To install and configure the PostgreSQL database on Linux, you can follow these steps:

  1. Install the PostgreSQL package: Run the following command in the terminal to install the PostgreSQL package:
sudo apt-get update
sudo apt-get install postgresql
  1. Start the PostgreSQL service: After installation, you can start the PostgreSQL service using the following command.
sudo service postgresql start
  1. Setting up PostgreSQL user and database: By default, PostgreSQL will create a user named “postgres” and a database named “postgres”. You can use the following command to log in to the PostgreSQL command line interface and create new users and databases:
sudo -u postgres psql

In the PostgreSQL command-line interface, you can use the following commands to create a new user and database:

CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydatabase OWNER myuser;
  1. Set up PostgreSQL access control: By default, PostgreSQL only allows local connections. To allow remote connections, you will need to edit the PostgreSQL configuration file. Open the configuration file:
sudo nano /etc/postgresql/<version>/main/pg_hba.conf

Add the following lines to the file to allow connections from specified IP addresses:

host    mydatabase    myuser    192.168.1.1/32    md5

Replace the above IP address with the actual IP address needed for connection.

  1. Restart the PostgreSQL service: After making configuration changes, restart the PostgreSQL service to apply the changes.
sudo service postgresql restart

By doing this, you have now successfully installed and configured the PostgreSQL database on Linux. You can now connect and operate using the user and database you created.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds