Install & Configure PostgreSQL on Amazon Linux
As a developer, having a reliable database management system is crucial for storing and managing data. PostgreSQL is one such popular open-source relational database management system that offers robust features and scalability. In this article, we will walk you through the process of installing and configuring PostgreSQL on Amazon Linux.
Installing PostgreSQL 14
To get started, we need to install PostgreSQL 14 on our Amazon Linux machine. We can do this by enabling the PostgreSQL 14 package using the following command:
# Install PostgreSQL
sudo amazon-linux-extras enable postgresql14 > /dev/null
sudo yum -y install postgresql postgresql-server postgresql-contrib postgresql-devel > /dev/null
These commands will download and install the necessary packages for PostgreSQL.
Configuring PostgreSQL Database & Starting Service
Once installed, we need to configure our PostgreSQL database. We can do this by setting environment variables:
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=sample_db
POSTGRES_USER=db_user
POSTGRES_PASSWORD=db_pass
Next, we need to initialize the PostgreSQL database and start the service:
sudo postgresql-setup initdb
sudo systemctl enable postgresql
sudo systemctl start postgresql
After that, we can create a new database, user, and grant privileges using the following commands:
sudo -u postgres psql -c "CREATE DATABASE ${POSTGRES_DB};"
sudo -u postgres psql -c "CREATE USER ${POSTGRES_USER} WITH PASSWORD '${POSTGRES_PASSWORD}';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO ${POSTGRES_USER};"
Finally, we need to update the PostgreSQL configuration file pg_hba.conf
to allow remote connections:
sudo sed -i 's|host all all 127.0.0.1/32 ident|host all all 127.0.0.1/32 md5|g' /var/lib/pgsql/data/pg_hba.conf
sudo systemctl restart postgresql
That's it! We have now successfully installed and configured PostgreSQL on our Amazon Linux machine.
Remember to replace the environment variables with your own values, such as database name, user, and password. With this setup, you're ready to start using PostgreSQL for your development needs.