|
1 | 1 | #!/bin/bash |
2 | | -set -e |
| 2 | +set -ex |
3 | 3 |
|
4 | | -# Get parameters |
| 4 | +# Default versions if not provided |
5 | 5 | PG_VERSION=${1:-17} |
6 | 6 | PGVECTOR_VERSION=${2:-0.8.0} |
7 | 7 | PGUSER=${3:-postgres} |
8 | 8 | PGPASSWORD=${4:-postgres} |
9 | 9 | PGDATABASE=${5:-postgres} |
10 | 10 |
|
11 | | -# Function to export variables |
12 | | -export_var() { |
13 | | - local name=$1 |
14 | | - local value=$2 |
15 | | - export "$name=$value" |
16 | | - # Only export to GITHUB_ENV if running in GitHub Actions |
17 | | - if [ -n "$GITHUB_ENV" ]; then |
18 | | - echo "$name=$value" >> $GITHUB_ENV |
19 | | - fi |
20 | | -} |
21 | | - |
22 | | -# Add PostgreSQL repository |
23 | | -curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg |
24 | | -echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list |
25 | | - |
26 | | -# Install PostgreSQL and build dependencies |
| 11 | +echo "Installing PostgreSQL ${PG_VERSION}..." |
| 12 | +sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y |
27 | 13 | sudo apt-get update |
28 | | -sudo apt-get install -y \ |
29 | | - "postgresql-$PG_VERSION" \ |
30 | | - "postgresql-server-dev-$PG_VERSION" \ |
31 | | - build-essential \ |
32 | | - git |
33 | | - |
34 | | -# Ensure PostgreSQL is started |
35 | | -sudo systemctl enable "postgresql@$PG_VERSION-main" |
36 | | -sudo systemctl start "postgresql@$PG_VERSION-main" |
37 | | - |
38 | | -# Configure PostgreSQL authentication |
39 | | -sudo sed -i 's/peer/trust/g' "/etc/postgresql/$PG_VERSION/main/pg_hba.conf" |
40 | | -sudo sed -i 's/scram-sha-256/trust/g' "/etc/postgresql/$PG_VERSION/main/pg_hba.conf" |
41 | | -sudo systemctl restart "postgresql@$PG_VERSION-main" |
42 | | - |
43 | | -# Wait for PostgreSQL to start |
44 | | -for i in {1..10}; do |
45 | | - if sudo -u postgres psql -c '\l' >/dev/null 2>&1; then |
46 | | - break |
47 | | - fi |
48 | | - echo "Waiting for PostgreSQL to start... ($i/10)" |
49 | | - sleep 1 |
50 | | -done |
51 | | - |
52 | | -# Create user and set password |
53 | | -sudo -u postgres psql -c "CREATE USER $PGUSER WITH SUPERUSER PASSWORD '$PGPASSWORD';" || true |
54 | | -sudo -u postgres psql -c "ALTER USER $PGUSER WITH PASSWORD '$PGPASSWORD';" |
55 | | - |
56 | | -# Create database if it doesn't exist |
57 | | -if [ "$PGDATABASE" != "postgres" ]; then |
58 | | - sudo -u postgres createdb -O "$PGUSER" "$PGDATABASE" || true |
59 | | -fi |
| 14 | +sudo apt-get install -y postgresql-${PG_VERSION} |
| 15 | + |
| 16 | +# Configure PostgreSQL |
| 17 | +sudo systemctl stop postgresql |
| 18 | +sudo pg_dropcluster --stop ${PG_VERSION} main || true |
| 19 | +sudo pg_createcluster ${PG_VERSION} main --start || true |
| 20 | +sudo systemctl start postgresql |
| 21 | + |
| 22 | +# Create runner user and grant permissions |
| 23 | +sudo -u postgres psql -c "CREATE USER runner WITH SUPERUSER;" |
| 24 | + |
| 25 | +# Verify PostgreSQL installation and version |
| 26 | +echo "Checking PostgreSQL version..." |
| 27 | +PG_ACTUAL_VERSION=$(sudo -u postgres psql -t -c "SHOW server_version;" | xargs) |
| 28 | +echo "PostgreSQL actual version: ${PG_ACTUAL_VERSION}" |
| 29 | +PG_MAJOR_VERSION=$(echo ${PG_ACTUAL_VERSION} | cut -d. -f1) |
| 30 | +echo "PostgreSQL major version: ${PG_MAJOR_VERSION}" |
60 | 31 |
|
61 | | -# Build and install pgvector |
62 | | -git clone --branch "v$PGVECTOR_VERSION" https://github.com/pgvector/pgvector.git |
| 32 | +# Remove any existing pgvector installations |
| 33 | +sudo apt-get remove -y postgresql-*-pgvector || true |
| 34 | +sudo rm -f /usr/lib/postgresql/*/lib/vector.so |
| 35 | +sudo rm -f /usr/share/postgresql/*/extension/vector* |
| 36 | + |
| 37 | +echo "Installing pgvector..." |
| 38 | +# Always build from source to match PostgreSQL version |
| 39 | +echo "Building pgvector from source..." |
| 40 | +sudo apt-get install -y postgresql-server-dev-${PG_MAJOR_VERSION} build-essential git |
| 41 | +git clone --branch v${PGVECTOR_VERSION} https://github.com/pgvector/pgvector.git |
63 | 42 | cd pgvector |
64 | 43 | make clean |
65 | | -sudo make install |
| 44 | +PG_CONFIG=/usr/lib/postgresql/${PG_MAJOR_VERSION}/bin/pg_config make |
| 45 | +sudo PG_CONFIG=/usr/lib/postgresql/${PG_MAJOR_VERSION}/bin/pg_config make install |
66 | 46 | cd .. |
67 | 47 | rm -rf pgvector |
68 | 48 |
|
| 49 | +# Configure PostgreSQL authentication for CI |
| 50 | +echo "local all postgres trust" | sudo tee /etc/postgresql/${PG_VERSION}/main/pg_hba.conf |
| 51 | +echo "local all runner trust" | sudo tee -a /etc/postgresql/${PG_VERSION}/main/pg_hba.conf |
| 52 | +echo "local all all trust" | sudo tee -a /etc/postgresql/${PG_VERSION}/main/pg_hba.conf |
| 53 | + |
| 54 | +# Restart PostgreSQL to ensure pgvector is loaded |
| 55 | +sudo systemctl restart postgresql |
| 56 | + |
| 57 | +# Verify pgvector installation |
| 58 | +echo "Verifying pgvector installation..." |
| 59 | +echo "Installed extensions:" |
| 60 | +sudo -u postgres psql -d postgres -c "SELECT * FROM pg_extension;" || true |
| 61 | +echo "Available extensions:" |
| 62 | +sudo -u postgres psql -d postgres -c "SELECT * FROM pg_available_extensions WHERE name = 'vector';" || true |
| 63 | + |
| 64 | +# List extension directory contents |
| 65 | +echo "Checking extension files..." |
| 66 | +echo "PostgreSQL ${PG_MAJOR_VERSION} extension directory:" |
| 67 | +ls -la /usr/share/postgresql/${PG_MAJOR_VERSION}/extension/ || true |
| 68 | +echo "PostgreSQL ${PG_MAJOR_VERSION} lib directory:" |
| 69 | +ls -la /usr/lib/postgresql/${PG_MAJOR_VERSION}/lib/ || true |
| 70 | + |
| 71 | +# Set password and create database |
| 72 | +sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '$PGPASSWORD';" |
| 73 | +if [ "$PGUSER" != "postgres" ]; then |
| 74 | + sudo -u postgres createuser -s $PGUSER |
| 75 | + sudo -u postgres psql -c "ALTER USER $PGUSER WITH PASSWORD '$PGPASSWORD';" |
| 76 | +fi |
| 77 | +if [ "$PGDATABASE" != "postgres" ]; then |
| 78 | + sudo -u postgres createdb -O $PGUSER $PGDATABASE |
| 79 | +fi |
| 80 | + |
69 | 81 | # Create and configure pgvector extension |
70 | | -PGPASSWORD=$PGPASSWORD psql -h localhost -U "$PGUSER" -d "$PGDATABASE" -c 'CREATE EXTENSION IF NOT EXISTS vector;' |
| 82 | +sudo -u postgres psql -d $PGDATABASE -c "CREATE EXTENSION IF NOT EXISTS vector;" |
71 | 83 |
|
72 | 84 | # Export environment variables |
73 | | -export_var "PGHOST" "localhost" |
74 | | -export_var "PGUSER" "$PGUSER" |
75 | | -export_var "PGPASSWORD" "$PGPASSWORD" |
76 | | -export_var "PGDATABASE" "$PGDATABASE" |
77 | | - |
78 | | -# Print success message |
79 | | -echo "PostgreSQL and pgvector have been successfully installed!" |
80 | | -echo "Connection details:" |
81 | | -echo " Host: localhost" |
82 | | -echo " User: $PGUSER" |
83 | | -echo " Database: $PGDATABASE" |
84 | | -echo " Password: [hidden]" |
| 85 | +export PGHOST=localhost |
| 86 | +export PGUSER=$PGUSER |
| 87 | +export PGPASSWORD=$PGPASSWORD |
| 88 | +export PGDATABASE=$PGDATABASE |
| 89 | + |
| 90 | +# Export to GITHUB_ENV only if running in GitHub Actions |
| 91 | +if [ -n "$GITHUB_ENV" ]; then |
| 92 | + echo "PGHOST=$PGHOST" >> $GITHUB_ENV |
| 93 | + echo "PGUSER=$PGUSER" >> $GITHUB_ENV |
| 94 | + echo "PGPASSWORD=$PGPASSWORD" >> $GITHUB_ENV |
| 95 | + echo "PGDATABASE=$PGDATABASE" >> $GITHUB_ENV |
| 96 | +fi |
| 97 | + |
| 98 | +# Verify installation |
| 99 | +echo "Checking PostgreSQL installation..." |
| 100 | +psql -d $PGDATABASE -c "SELECT version();" |
| 101 | +echo "Checking available extensions..." |
| 102 | +psql -d $PGDATABASE -c "SELECT * FROM pg_available_extensions WHERE name = 'vector';" |
| 103 | +echo "Checking installed extensions..." |
| 104 | +psql -d $PGDATABASE -c "SELECT * FROM pg_extension WHERE extname = 'vector';" |
0 commit comments