How to install and configure PostgreSQL database on Linux?
To install and configure the PostgreSQL database on Linux, you can follow these steps:
- 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
- Start the PostgreSQL service: After installation, you can start the PostgreSQL service using the following command.
sudo service postgresql start
- 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;
- 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.
- 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.