Download the latest release from Releases
The installation package comes bundled with all essential components: the PostgreSQL server, command-line tools, and optional administrative utilities. Be sure to download the edition that matches your operating system.
PostgreSQL is a robust, open-source, object-relational database system that has undergone more than 35 years of continuous development. It is widely respected for its reliability, adherence to standards, and flexibility.
Built to accommodate various types of workloads, PostgreSQL is compatible with all major operating systems and is used in everything from lightweight web applications to enterprise-level data warehouses and mission-critical environments.
This repository contains the primary source code for PostgreSQL. For help with installation, documentation, or contribution guidelines, check the official website or browse the /doc
directory.
- Connect to PostgreSQL
- Creating a Database
- Connecting to a Database
- Security and Authentication
- Backup and Recovery
PostgreSQL includes a graphical interface utility called pgAdmin.
To connect:
-
Launch pgAdmin 4.
-
Right-click Servers → Create → Server.
-
Specify a name for the server (e.g.,
Localhost
). -
Under the Connection tab, enter the following:
- Host:
localhost
- Port:
5432
- Username:
postgres
- Password: (the one you set during installation)
- Host:
Click Save — this will establish the connection.
Once PostgreSQL is installed and operational on Windows, creating a new database is a fast and straightforward process. Depending on your preference, you can use either the graphical interface (pgAdmin) or the terminal-based client (psql).
pgAdmin is the default graphical interface tool that ships with PostgreSQL for Windows.
-
Open pgAdmin 4 from the Start Menu.
-
Connect to your local server:
- Host:
localhost
- Port:
5432
- Username:
postgres
- Enter your installation password
- Host:
-
In the Object Browser, right-click Databases → Create → Database...
-
Provide the following details:
- Database name: e.g.,
mydatabase
- Owner: typically
postgres
- Database name: e.g.,
-
Click Save.
The psql
client is a terminal-based tool for working with PostgreSQL, and it also supports database creation.
-
Open the Command Prompt.
-
Change directory to the PostgreSQL
bin
folder:cd "C:\Program Files\PostgreSQL\<version>\bin"
-
Start the
psql
terminal:psql -U postgres -h localhost
You’ll be prompted to enter your password.
-
Inside
psql
, run the command to create a new database:CREATE DATABASE mydatabase;
-
Database names must start with a letter and can contain letters, numbers, and underscores.
-
To display all available databases:
\l
-
To delete a database:
DROP DATABASE mydatabase;
Only the owner of the database or a superuser has the rights to drop it.
To establish a connection to a PostgreSQL database via command line:
psql -U postgres -d mydatabase
Once inside psql
, you can switch between databases by executing:
\c mydatabase;
INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
SELECT * FROM users;
UPDATE users SET email = 'john.doe@example.com' WHERE name = 'John Doe';
DELETE FROM users WHERE name = 'John Doe';
Indexes can significantly speed up query execution. To define an index:
CREATE INDEX idx_users_email ON users(email);
To analyze performance and execution details:
EXPLAIN ANALYZE SELECT * FROM users WHERE email='john@example.com';
To create a new role:
CREATE ROLE dbuser WITH LOGIN PASSWORD 'securepassword';
To assign privileges:
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO dbuser;
To change authentication settings, modify pg_hba.conf
:
sudo nano /etc/postgresql/16/main/pg_hba.conf
pg_dump -U postgres -d mydatabase -f backup.sql
psql -U postgres -d mydatabase -f backup.sql
PostgreSQL supports replication features that help increase reliability and uptime.
- In
postgresql.conf
on the primary server:
wal_level = replica
max_wal_senders = 5
- In
pg_hba.conf
, define access for the replica:
host replication replicator 192.168.1.10/32 md5
- Initialize the replica setup:
pg_basebackup -h primary_host -D /var/lib/postgresql/16/main -U replicator -P -R