← Back to postgres/postgres

How to Deploy & Use postgres/postgres

PostgreSQL Deployment and Usage Guide

1. Prerequisites

System Requirements

  • Operating System: Most Unix-like systems (Linux, BSD, macOS, Solaris) or Windows
  • Disk Space: Approximately 100MB for source code, plus space for your databases
  • Memory: Minimum 128MB RAM (1GB+ recommended for production)

Required Development Tools

  • C Compiler: GCC or Clang (C99 compliant)
  • GNU Make: Version 3.80 or later
  • Flex: Version 2.5.31 or later (for building from Git)
  • Bison: Version 1.875 or later (for building from Git)
  • Perl: Version 5.8 or later (for building from Git)
  • Python: Version 3 or later (for some test scripts)

Optional Dependencies (Recommended)

  • readline library: For command-line editing in psql
  • zlib library: For compression support
  • OpenSSL: For encrypted connections
  • libxml2: For XML support
  • libxslt: For XSLT support

System Accounts

  • A non-root user account for building and running PostgreSQL
  • The system will create a postgres user account during installation

2. Installation

Clone the Repository

# Clone the PostgreSQL mirror repository
git clone https://github.com/postgres/postgres.git

# Navigate to the source directory
cd postgres

Important Note: This is a mirror repository. To contribute patches, please follow the process at https://wiki.postgresql.org/wiki/Submitting_a_Patch instead of using GitHub pull requests.

Configure the Build

# Run the configure script (in the source directory)
./configure

# For custom installation directory
./configure --prefix=/usr/local/pgsql

# Common options:
./configure --with-openssl      # Enable SSL support
./configure --with-perl         # Enable Perl support
./configure --with-python       # Enable Python support
./configure --with-libxml       # Enable XML support

Install PostgreSQL

# Compile the source code
make

# Run regression tests (optional but recommended)
make check

# Install to the configured prefix (requires appropriate permissions)
sudo make install

# Install documentation (optional)
sudo make install-docs

Create a Database Cluster

# Add PostgreSQL binaries to your PATH
export PATH=/usr/local/pgsql/bin:$PATH

# Create a database cluster (data directory)
initdb -D /usr/local/pgsql/data

# Or specify a different location
initdb -D /path/to/your/data/directory

3. Configuration

Environment Variables

# Add to your shell profile (~/.bashrc, ~/.zshrc, etc.)
export PATH=/usr/local/pgsql/bin:$PATH
export PGDATA=/usr/local/pgsql/data
export PGPORT=5432
export PGHOST=localhost

Configuration Files

Primary configuration files located in your data directory ($PGDATA):

  1. postgresql.conf - Main server configuration

    # Common settings to adjust:
    listen_addresses = 'localhost'          # Change to '*' for network access
    port = 5432                            # Default PostgreSQL port
    max_connections = 100                  # Maximum concurrent connections
    shared_buffers = 128MB                 # Memory for shared buffers
    
  2. pg_hba.conf - Client authentication configuration

    # Example entries:
    # TYPE  DATABASE  USER  ADDRESS  METHOD
    local   all       all             trust
    host    all       all   127.0.0.1/32  md5
    host    all       all   ::1/128       md5
    
  3. pg_ident.conf - User name mapping

No API Keys Required

PostgreSQL doesn't require external API keys. Authentication is managed through:

  • Password authentication (md5, scram-sha-256)
  • Peer authentication (local connections)
  • Certificate authentication
  • LDAP, RADIUS, or PAM (if configured)

4. Build & Run

Development Build

# From the source directory, build with debugging symbols
./configure --enable-debug --enable-cassert
make
sudo make install

# Run the server in foreground for debugging
postgres -D /usr/local/pgsql/data --log-stderr

Production Build

# Optimized build for production
./configure --with-openssl --with-perl --with-python CFLAGS="-O2"
make world                    # Build everything including contrib modules
sudo make install-world

# Enable data checksums for corruption detection
initdb -D /data/pgdata --data-checksums

Start the Server

# Manual start
pg_ctl -D /usr/local/pgsql/data start

# Start with log file
pg_ctl -D /usr/local/pgsql/data -l logfile start

# Stop the server
pg_ctl -D /usr/local/pgsql/data stop

Connect and Create Database

# Connect to the default postgres database
psql postgres

# Create a new database
createdb mydatabase

# Connect to your new database
psql mydatabase

# Create a user
createuser myuser --interactive

5. Deployment

Platform Recommendations

Self-Managed Servers:

  • Linux (Recommended): Ubuntu LTS, RHEL/CentOS, Debian
  • BSD Systems: FreeBSD, OpenBSD
  • Windows Server: Native Windows version available

Cloud Platforms:

  • AWS: EC2 instances, RDS PostgreSQL
  • Google Cloud: Compute Engine, Cloud SQL
  • Azure: Virtual Machines, Azure Database for PostgreSQL
  • DigitalOcean: Droplets with PostgreSQL one-click app

Containerized Deployment:

# Using official PostgreSQL Docker image
docker run --name postgres \
  -e POSTGRES_PASSWORD=mysecretpassword \
  -e POSTGRES_USER=myuser \
  -e POSTGRES_DB=mydatabase \
  -p 5432:5432 \
  -v /path/to/data:/var/lib/postgresql/data \
  postgres:latest

Production Deployment Checklist

  1. Security Hardening:

    # Change default port (optional)
    port = 5433
    
    # Enable SSL
    ssl = on
    ssl_cert_file = 'server.crt'
    ssl_key_file = 'server.key'
    
  2. Performance Tuning:

    # In postgresql.conf
    shared_buffers = 25% of RAM (max 8GB)
    effective_cache_size = 50-75% of RAM
    maintenance_work_mem = 10% of RAM
    checkpoint_completion_target = 0.9
    
  3. Backup Configuration:

    # Enable WAL archiving
    archive_mode = on
    archive_command = 'cp %p /path/to/wal_archive/%f'
    
    # Set up pg_basebackup for physical backups
    pg_basebackup -D /backup/location -Ft -Xs -P
    

6. Troubleshooting

Common Issues and Solutions

Server won't start:

# Check the log file
tail -f /usr/local/pgsql/data/log/postgresql-*.log

# Common permission issue
chmod 700 /usr/local/pgsql/data
chown postgres:postgres /usr/local/pgsql/data -R

Connection refused:

# Check if PostgreSQL is listening
netstat -an | grep 5432

# Ensure listen_addresses is not 'localhost' only
# In postgresql.conf: listen_addresses = '*'

Authentication failures:

# Check pg_hba.conf configuration
cat /usr/local/pgsql/data/pg_hba.conf

# Reload configuration without restart
pg_ctl -D /usr/local/pgsql/data reload

Out of memory errors:

# Reduce memory settings in postgresql.conf
shared_buffers = 128MB
work_mem = 4MB
maintenance_work_mem = 64MB

Build failures:

# Missing dependencies - install development packages
# On Ubuntu/Debian:
sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison

# On RHEL/CentOS:
sudo yum groupinstall "Development Tools"
sudo yum install readline-devel zlib-devel flex bison

Port already in use:

# Find what's using port 5432
sudo lsof -i :5432

# Kill the process or change PostgreSQL port
# In postgresql.conf: port = 5433

Getting Help

For detailed installation instructions, always refer to the official documentation at https://www.postgresql.org/docs/devel/installation.html