Skip to content

Instantly share code, notes, and snippets.

@flavienbwk
Created September 13, 2024 03:44
Show Gist options
  • Save flavienbwk/f1b9e122931fd92ed566cfaf9934d89f to your computer and use it in GitHub Desktop.
Save flavienbwk/f1b9e122931fd92ed566cfaf9934d89f to your computer and use it in GitHub Desktop.
install-pgvector.sh
#!/bin/bash
# Install PostgreSQL + pgvector + enable SSL connectivity
PGPASSWORD=somepassword
PGUSER=myuser
PGDB=mydb
# Update package list
sudo apt update
# Install PostgreSQL and necessary build tools
sudo apt install -y postgresql postgresql-contrib build-essential postgresql-server-dev-all git
# Enable PostgreSQL service to start on boot
sudo systemctl enable postgresql
# Stop PostgreSQL service before making configuration changes
sudo systemctl stop postgresql
# Change PostgreSQL port to 16601
sudo sed -i "s/#port = 5432/port = 16601/" /etc/postgresql/*/main/postgresql.conf
# Configure PostgreSQL to allow remote connections
echo "listen_addresses = '*'" | sudo tee -a /etc/postgresql/*/main/postgresql.conf
# Allowing authentication for the user in pg_hba.conf
echo "host all $PGUSER 0.0.0.0/0 md5" | sudo tee -a /etc/postgresql/*/main/pg_hba.conf
# Start PostgreSQL service
sudo systemctl start postgresql
# Create database and user
sudo -u postgres psql -p 16601 -c "CREATE DATABASE $PGUSER;"
sudo -u postgres psql -p 16601 -c "CREATE USER $PGUSER WITH PASSWORD '$PGPASSWORD';"
sudo -u postgres psql -p 16601 -c "GRANT ALL PRIVILEGES ON DATABASE $PGDB TO $PGUSER;"
# Update PostgreSQL configuration to require SSL
echo "ssl = on" | sudo tee -a /etc/postgresql/*/main/postgresql.conf
echo "ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'" | sudo tee -a /etc/postgresql/*/main/postgresql.conf
echo "ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'" | sudo tee -a /etc/postgresql/*/main/postgresql.conf
# Install pgvector
cd /tmp
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git
cd pgvector
make
sudo make install
# Enable pgvector extension
sudo -u postgres psql -p 16601 -d $PGDB -c "CREATE EXTENSION vector;"
# Restart PostgreSQL service to apply changes
sudo systemctl restart postgresql
# Update firewall rules to allow connections on port 16601 (if UFW is enabled)
sudo ufw allow 16601/tcp
# Display connection string
echo "PostgreSQL with pgvector is installed and configured. Connect using: postgresql://$PGUSER:$PGPASSWORD@localhost:16601/$PGDB?sslmode=require"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment