19 September 2018

Installing PostgreSQL 9.6 on macOS 10.13.6 via MacPorts for Development Purposes

After finally receiving a new MacBook Pro with 2.9 Ghz, 6-core i9 processor and 32gb of memory, I am reinstalling all of my development tools. Not only is the big increase in resources really useful from the development point of view, but the computer is lighter and thinner as well. That being said, there are still things I do not like, such as the keyboard, the touchbar (I miss the tactile feedback from the escape key which, as a Vim user, I use constantly) and some other minor things. For my own purposes, I usually document the steps to make it easier in the future. But I also figured it might help other folks out there to achieve the same ends.


Having documented this procedure once or twice before, I'm going to keep this brief.



$ $ sudo port install postgresql96-server
---> Computing dependencies for postgresql96-server
The following dependencies will be installed:
libxml2
libxslt
postgresql96
postgresql_select
Continue? [Y/n]:
---> Fetching archive for libxml2
---> Attempting to fetch libxml2-2.9.7_0.darwin_17.x86_64.tbz2 from https://packages.macports.org/libxml2
---> Attempting to fetch libxml2-2.9.7_0.darwin_17.x86_64.tbz2.rmd160 from https://packages.macports.org/libxml2
---> Installing libxml2 @2.9.7_0
---> Activating libxml2 @2.9.7_0
---> Cleaning libxml2
---> Fetching archive for libxslt
---> Attempting to fetch libxslt-1.1.32_0.darwin_17.x86_64.tbz2 from https://packages.macports.org/libxslt
---> Attempting to fetch libxslt-1.1.32_0.darwin_17.x86_64.tbz2.rmd160 from https://packages.macports.org/libxslt
---> Installing libxslt @1.1.32_0
---> Activating libxslt @1.1.32_0
---> Cleaning libxslt
---> Fetching archive for postgresql_select
---> Attempting to fetch postgresql_select-0.3_0.darwin_17.noarch.tbz2 from https://packages.macports.org/postgresql_select
---> Attempting to fetch postgresql_select-0.3_0.darwin_17.noarch.tbz2.rmd160 from https://packages.macports.org/postgresql_select
---> Installing postgresql_select @0.3_0
---> Activating postgresql_select @0.3_0
---> Cleaning postgresql_select
---> Fetching archive for postgresql96
---> Attempting to fetch postgresql96-9.6.10_0.darwin_17.x86_64.tbz2 from https://packages.macports.org/postgresql96
---> Attempting to fetch postgresql96-9.6.10_0.darwin_17.x86_64.tbz2.rmd160 from https://packages.macports.org/postgresql96
---> Installing postgresql96 @9.6.10_0
---> Activating postgresql96 @9.6.10_0
---> Cleaning postgresql96
---> Fetching archive for postgresql96-server
---> Attempting to fetch postgresql96-server-9.6.10_0.darwin_17.noarch.tbz2 from https://packages.macports.org/postgresql96-server
---> Attempting to fetch postgresql96-server-9.6.10_0.darwin_17.noarch.tbz2.rmd160 from https://packages.macports.org/postgresql96-server
---> Installing postgresql96-server @9.6.10_0
---> Activating postgresql96-server @9.6.10_0
---> Cleaning postgresql96-server
---> Updating database of binaries
---> Updating database of C++ stdlib usage
---> Scanning binaries for linking errors
---> No broken files found.
---> No broken ports found.
---> Some of the ports you installed have notes:
postgresql96 has the following notes:
To use the postgresql server, install the postgresql96-server port
postgresql96-server has the following notes:
To create a database instance, after install do
sudo mkdir -p /opt/local/var/db/postgresql96/defaultdb
sudo chown postgres:postgres /opt/local/var/db/postgresql96/defaultdb
sudo su postgres -c 'cd /opt/local/var/db/postgresql96 && /opt/local/lib/postgresql96/bin/initdb -D /opt/local/var/db/postgresql96/defaultdb'
A startup item has been generated that will aid in starting postgresql96-server with launchd. It is disabled by default. Execute the following command to start it, and to cause it to launch at startup:
sudo port load postgresql96-server
$
$ sudo mkdir -p /opt/local/var/db/postgresql96/defaultdb
$ sudo chown postgres:postgres /opt/local/var/db/postgresql96/defaultdb
$ sudo su postgres -c 'cd /opt/local/var/db/postgresql96 && /opt/local/lib/postgresql96/bin/initdb -D /opt/local/var/db/postgresql96/defaultdb'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /opt/local/var/db/postgresql96/defaultdb ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/opt/local/lib/postgresql96/bin/pg_ctl -D /opt/local/var/db/postgresql96/defaultdb -l logfile start
$
$ sudo su postgres -c '/opt/local/lib/postgresql96/bin/pg_ctl -D /opt/local/var/db/postgresql96/defaultdb -l /opt/local/var/log/postgresql96/postgres.log start &'
server starting
$ sudo -u postgres /opt/local/lib/postgresql96/bin/createuser -d bsnyder
$ sudo -u postgres /opt/local/lib/postgresql96/bin/createdb bsnyder
$ sudo -u postgres /opt/local/lib/postgresql96/bin/psql
postgres=# alter user bsnyder with encrypted password 'bsnyder';
ALTER ROLE
postgres=# grant all privileges on database bsnyder to bsnyder;
GRANT
postgres=# \q
$ /opt/local/lib/postgresql96/bin/psql -U bsnyder
psql (9.6.10)
Type "help" for help.
bsnyder=> select version();
version
-----------------------------------------------------------------------------------------------------------------
PostgreSQL 9.6.10 on x86_64-apple-darwin17.7.0, compiled by Apple LLVM version 9.1.0 (clang-902.0.39.2), 64-bit
(1 row)
bsnyder=> \q
$



BTW, below are the start/stop scripts that I created for PostgreSQL:

#!/bin/sh
sudo su - postgres -c '/opt/local/lib/postgresql96/bin/pg_ctl -D /opt/local/var/db/postgresql96/defaultdb -l /opt/local/var/log/postgresql96/postgres.log start &'
pg_version=$(psql --version | cut -d ' ' -f3)
if [ $? -eq 0 ]; then
echo "PostgreSQL $pg_version started successfully"
exit 0
else
echo "PostgreSQL $pg_version did not start in a timely fashion, please see /opt/local/var/log/postgresql96/postgres.log for details"
exit 1
fi
view raw pg_start.sh hosted with ❤ by GitHub


#!/bin/sh
sudo su - postgres -c '/opt/local/lib/postgresql96/bin/pg_ctl -D /opt/local/var/db/postgresql96/defaultdb -l /opt/local/var/log/postgresql96/postgres.log stop'
pg_version=$(psql --version | cut -d ' ' -f3)
if [ $? -eq 0 ]; then
echo "PostgreSQL $pg_version stopped successfully"
exit 0
else
echo "PostgreSQL $pg_version did not stop in a timely fashion, please see /opt/local/var/log/postgresql96/postgres.log for details"
exit 1
fi
view raw pg_stop.sh hosted with ❤ by GitHub

No comments:

Post a Comment