SoFunction
Updated on 2025-04-24

The most detailed installation PostgreSQL method and common problems solved

1. Install PostgreSQL on Windows system

1. Download the PostgreSQL installation package

  • Visit the PostgreSQL official website download page:/download/windows/
  • choose“Windows”For version, click Download and enter the EnterpriseDB installation page.
  • Download the latest version of the installation package (.exe file).

2. Install PostgreSQL

  • Double-click the downloaded installation package (.exe file).
  • In the installation wizard that pops up, click Next to continue.
  • Select the installation directory (the default installation path is C:\Program Files\PostgreSQL\xx, which can be changed, but the default path is recommended).
  • Select the installed components, and by default all components will be checked (including PostgreSQL Server, pgAdmin, Command Line Tools, StackBuilder). Generally, just keep the default settings, click Next.
  • Set up the data directory of the database cluster. This directory will store all database data files. You can select the default path or the custom path and click Next.
  • Set the superuser password (i.e. the password of the postgres user). Please remember this password, which you will need to use later.
  • Select the database port (default is 5432), generally no modification is required, click Next.
  • Select the language settings, usually select English, and click Next.
  • Click Next, and Install to start installing PostgreSQL.
  • After the installation is complete, click Finish to complete the installation.

3. Verify PostgreSQL installation

  • After the installation is complete, you can connect to the database through pgAdmin (a graphical interface tool provided by PostgreSQL).
  • Start pgAdmin, enter the password set during installation, and connect to the local database.
  • You can verify that the database is running normally through the SQL query tool:
SELECT version();

4. Configure PostgreSQL startup items

If you want PostgreSQL to start automatically when Windows starts, you can configure it by following the steps:

  • OpenService Manager(according toWin + R,enter)。
  • Find the PostgreSQL service (usually namedpostgresql-x64-xx)。
  • Right-click and selectproperty. existStartup TypeSelected inautomatic, and clickapplicationandSure

2. Install PostgreSQL on Linux system

1. Install PostgreSQL

For Ubuntu/Debian systems:

Update the system:

sudo apt update

Install PostgreSQL:

sudo apt install postgresql postgresql-contrib

For CentOS/RHEL system: Update the system:

sudo yum update

Install PostgreSQL:

sudo yum install postgresql-server postgresql-contrib

Initialize the database:

sudo postgresql-setup initdb

Start the PostgreSQL service:

sudo systemctl start postgresql

Set PostgreSQL to start automatically when powered on:

sudo systemctl enable postgresql

2. Configure PostgreSQL

PostgreSQL is used by defaultpeerAuthentication method to allow operating system users to log in. If you want to use password authentication, you need to modify the PostgreSQL configuration file:

sudo nano /var/lib/pgsql/data/pg_hba.conf

Find the following line:

local   all             postgres                                peer

WillpeerChange tomd5, then save and exit. Restart the PostgreSQL service to apply the configuration:

sudo systemctl restart postgresql

3. Set up PostgreSQL

Super User Password Switch to PostgreSQL user:

sudo -i -u postgres

Enter the PostgreSQL command line interface:

psql

set uppostgresUser password:

ALTER USER postgres WITH PASSWORD 'yourpassword';

Exit the PostgreSQL command line:

\q

4. Verify the installation

You can verify that PostgreSQL is installed successfully and works properly using the following command:

psql -U postgres -h localhost -d postgres

3. Install PostgreSQL on macOS system

1. Install PostgreSQL using Homebrew If Homebrew has not been installed yet, please install it first:

/bin/bash -c "$(curl -fsSL /Homebrew/install/HEAD/)"

Install PostgreSQL:

brew install postgresql

2. Start PostgreSQL Start PostgreSQL:

brew services start postgresql

Set PostgreSQL to start automatically when powered on:

brew services start postgresql

3. Set PostgreSQL Superuser Password

Switch to PostgreSQL user:

psql postgres

set uppostgresUser password:

ALTER USER postgres WITH PASSWORD 'yourpassword';

Exit the PostgreSQL command line:

\q

4. Verify the installation

You can verify that PostgreSQL is installed successfully and works properly through the following command:

psql -U postgres -h localhost -d postgres

4. Frequently Asked Questions

1. Unable to connect to PostgreSQL database

Confirm that the PostgreSQL service is running:

sudo systemctl status postgresql

Make sure the firewall is not blocking PostgreSQL default port (5432).

2. Cannot connect via pgAdmin

make surepg_hba.confThe configuration file has been set tomd5Certification. Check PostgreSQL configuration fileIn-houselisten_addressesWhether to set to'*', that is, all IP connections are allowed.

This is the article about the most detailed installation of PostgreSQL methods. For more related PostgreSQL installation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!