Postgresql install errors on Ubuntu 16.04

AI 生成的摘要

In a discussion about PostgreSQL installation errors on Ubuntu 16.04 while setting up Discourse, j127 reports encountering permission issues (e.g., PG::InsufficientPrivilege for creating extensions like hstore) and missing databases (e.g., discourse_test_multisite). They resolved the issues by modifying the setup script: switching from sudo su postgres to sudo su - postgres for proper environment loading, explicitly creating the discourse_test_multisite database, and ensuring hstore and pg_trgm extensions are installed in all three databases (discourse_development, discourse_test, and discourse_test_multisite). Phlegz adds that a similar error can occur if a pre-existing PostgreSQL role with the same username exists — in such cases, dropping that role before rerunning the setup steps resolves the issue. Both users suggest their fixes may be worth documenting for others facing similar problems.

While trying to install Discourse on my laptop (Ubuntu 16.04) using this guide, I got some errors like this:

PG::InsufficientPrivilege: ERROR: permission denied to create extension "hstore"

and:

rails aborted!
ActiveRecord::NoDatabaseError: FATAL:  database "discourse_test_multisite" does not exist

I got rid of the errors by changing this:

# Postgresql
sudo su postgres
createuser --createdb --superuser -Upostgres $(cat /tmp/username)
psql -c "ALTER USER $(cat /tmp/username) WITH PASSWORD 'password';"
psql -c "create database discourse_development owner $(cat /tmp/username) encoding 'UTF8' TEMPLATE template0;"
psql -c "create database discourse_test        owner $(cat /tmp/username) encoding 'UTF8' TEMPLATE template0;"
psql -d discourse_development -c "CREATE EXTENSION hstore;"
psql -d discourse_development -c "CREATE EXTENSION pg_trgm;"
exit

to this:

# Postgresql
sudo su - postgres
createuser --createdb --superuser -Upostgres $(cat /tmp/username)
psql -c "ALTER USER $(cat /tmp/username) WITH PASSWORD 'password';"
psql -c "create database discourse_development owner $(cat /tmp/username) encoding 'UTF8' TEMPLATE template0;"
psql -c "create database discourse_test        owner $(cat /tmp/username) encoding 'UTF8' TEMPLATE template0;"
psql -c "create database discourse_test_multisite        owner $(cat /tmp/username) encoding 'UTF8' TEMPLATE template0;"
psql -d discourse_development -c "CREATE EXTENSION hstore;"
psql -d discourse_development -c "CREATE EXTENSION pg_trgm;"
psql -d discourse_test -c "CREATE EXTENSION hstore;"
psql -d discourse_test -c "CREATE EXTENSION pg_trgm;"
psql -d discourse_test_multisite -c "CREATE EXTENSION hstore;"
psql -d discourse_test_multisite -c "CREATE EXTENSION pg_trgm;"
exit

Diff:

2c2
< sudo su postgres
---
> sudo su - postgres
6a7
> psql -c "create database discourse_test_multisite        owner $(cat /tmp/username) encoding 'UTF8' TEMPLATE template0;"
8a10,13
> psql -d discourse_test -c "CREATE EXTENSION hstore;"
> psql -d discourse_test -c "CREATE EXTENSION pg_trgm;"
> psql -d discourse_test_multisite -c "CREATE EXTENSION hstore;"
> psql -d discourse_test_multisite -c "CREATE EXTENSION pg_trgm;"

I’m too new to Rails/Discourse to know if it’s just my laptop or if I should add them to the docs, but I’ll leave the fix here in case someone else finds it useful. :slight_smile:

(If it’s something that should go in the docs, let me know and I could make a pull request.)

I was getting the same error but I realized my problem was that I initially had a role with my username so what I needed to do was to drop that role in postgres and then follow the steps again:

# Postgresql
sudo -u postgres -i
createuser --superuser -Upostgres $(cat /tmp/username)
psql -c “ALTER USER $(cat /tmp/username) WITH PASSWORD ‘password’;”
exit