Skip to content

Commit

Permalink
Add postgres setup to mac and linux set up scripts
Browse files Browse the repository at this point in the history
Summary:
This patch installs postgresql server as part of the overall khan setup
process. To allow our access to the database in our tests and dev
environment, we need to make a couple small tweaks to the default
settings.

In os x land, we need to create a postgres user since the only user
created by homebrew matches the logged in user.

In linux world, we need to relax the authentication requirements to
match the settings that homebrew provides - namely to allow local users
to login with any username.

Note that we create the postgres databases as Makefile step within
webapp (to keep all schema changes to that repo). That forthcoming
change will need to land before this one.

Test Plan:
To test my changes on os x, I uninstalled posgres using homebrew
manually, then ran the steps in `./mac-setup.sh` and verified that they
worked without error.

To test my changes on linux, I booted a blank ubuntu (18.04) container
using docker, with the devtools directory mounted as a volume:
``
docker run -v `/home/dhruv/khan/devtools`:/root/khan/devtools --rm -it
ubuntu
``

I then commented out all steps in `linux_setup.sh` except
`install_postgresql` and ran the following

``
install_postgresql runs
apt-get update
apt-get install -y sudo curl gnupg software-properties-common
cd /root/khan/devtools/khan-dotfiles
./linux-setup.sh
```

Reviewers: benkraft, Kai, csilvers

Reviewed By: Kai, csilvers

Subscribers: kphilip, aric, #classroom-be, csilvers

Differential Revision: https://phabricator.khanacademy.org/D54534
  • Loading branch information
dkapadia committed May 22, 2019
1 parent 181d778 commit 21c1302
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
19 changes: 19 additions & 0 deletions linux-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ install_watchman() {
fi
}

install_postgresql() {
# Instructions taken from
# https://pgdash.io/blog/postgres-11-getting-started.html
# Postgres 11 is not available in 18.04, so we need to add the pg apt repository.
curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc \
| sudo apt-key add -

sudo add-apt-repository -y "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -c -s`-pgdg main"
sudo apt-get update
sudo apt-get install -y postgresql-11 postgresql-contrib libpq-dev

# Set up authentication to allow connections from the postgres user with no
# password. This matches the authentication setup that homebrew installs on
# a mac. Unlike a mac, we do not need to create a postgres user manually.
sudo cp -av postgresql/pg_hba.conf "/etc/postgresql/11/main/pg_hba.conf"
sudo service postgresql restart
}

setup_clock() {
# This shouldn't be necessary, but it seems it is.
if ! grep -q 3.ubuntu.pool.ntp.org /etc/ntp.conf; then
Expand Down Expand Up @@ -288,5 +306,6 @@ install_protoc
install_watchman
setup_clock
config_inotify
install_postgresql

trap - EXIT
22 changes: 21 additions & 1 deletion mac-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,25 @@ install_node() {
fi
}

install_postgresql() {
if ! brew ls postgresql >/dev/null 2>&1; then
info "Installing postgresql\n"
brew install postgresql@11
else
success "postgresql already installed"
fi

# We create a postgres user locally that we use in test and dev.
if ! psql \
-tc "SELECT rolname from pg_catalog.pg_roles" postgres \
| grep -c 'postgres' > /dev/null 2>&1 ; then
info "Creating postgres user for dev\n"
psql --quiet -c "CREATE ROLE postgres LOGIN SUPERUSER;" postgres;
else
success "postgres user already created"
fi
}

install_nginx() {
info "Checking for nginx\n"
if ! type nginx >/dev/null 2>&1; then
Expand Down Expand Up @@ -343,7 +362,7 @@ install_protoc() {
}

install_watchman() {
if ! which watchman ; then
if ! which watchman >/dev/null 2>&1; then
update "Installing watchman..."
brew install watchman
fi
Expand Down Expand Up @@ -431,6 +450,7 @@ install_homebrew
install_slack
update_git
install_node
install_postgresql
install_nginx
install_image_utils
install_helpful_tools
Expand Down
13 changes: 13 additions & 0 deletions postgresql/pg_hba.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# We allow any connection by the postgres user to local addresses, without a
# password.

# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all postgres trust

# IPv4 local connections:
host all postgres 127.0.0.1/32 trust
# IPv6 local connections:
host all postgres ::1/128 trust


8 changes: 8 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ download_db_dump() {
fi
}

create_pg_databases() {
if [ "$WEBAPP" = true ]; then
echo "Creating postgres databases"
( cd "$REPOS_DIR/webapp" ; make pg_create )
fi
}

# Make sure we store userinfo so we can pass appropriately when ka-cloning.
update_userinfo() {
echo "Updating your git user info"
Expand Down Expand Up @@ -393,6 +400,7 @@ install_deps # pre-reqs: clone_repos, install_and_setup_gcloud
install_hooks # pre-req: clone_repos
setup_arc # pre-req: clone_repos
download_db_dump # pre-req: install_deps
create_pg_databases # pre-req: install_deps


echo
Expand Down

0 comments on commit 21c1302

Please sign in to comment.