Skip to content

Instantly share code, notes, and snippets.

@jfreeze
Last active December 4, 2024 13:55
Show Gist options
  • Save jfreeze/ab8cf3bb1aaa05d5f2fdc0aadec8c8bb to your computer and use it in GitHub Desktop.
Save jfreeze/ab8cf3bb1aaa05d5f2fdc0aadec8c8bb to your computer and use it in GitHub Desktop.
Create a Postgres Database with Horizon
#
# Postgres Database Creation Example using Horizon
#
#
# Setup a host alias to your FreeBSD instance.
# Example host creation at https://hexdocs.pm/horizon/readme.html
#
$ sudo sh -c 'echo "178.156.153.23\t\tdemo-pg" >> /etc/hosts'
#
# Postgres install file
#
$ cat postgres.conf
pkg:postgresql17-server
pkg:postgresql17-contrib
postgres.zfs-init
postgres.init
postgres.db:c_mixed_utf8:my_app_prod
#
# Install Postgres
#
$ bsd_install.sh admin@demo-pg postgres.conf
[INFO] Installing postgres.conf on admin@demo-pg
[DEBUG] Generated installation arguments: --pkg postgresql17-server --pkg postgresql17-contrib --postgres-zfs-init --postgres-init --postgres-db-c_mixed_utf8 my_app_prod
Updating FreeBSD repository catalogue...
Fetching data.pkg: .......... done
Processing entries: .......... done
FreeBSD repository update completed. 35561 packages processed.
All repositories are up to date.
The following 16 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
gettext-runtime: 0.22.5
icu: 74.2_1,1
indexinfo: 0.3.1
libedit: 3.1.20240808,1
libffi: 3.4.6
liblz4: 1.10.0,1
libxml2: 2.11.9
llvm15: 15.0.7_10
lua53: 5.3.6_1
mpdecimal: 4.0.0
perl5: 5.36.3_2
postgresql17-client: 17.2
postgresql17-server: 17.2
python311: 3.11.10
readline: 8.2.13_1
zstd: 1.5.6
Number of packages to be installed: 16
The process will require 1 GiB more space.
231 MiB to be downloaded.
[1/16] Fetching indexinfo-0.3.1.pkg: . done
[2/16] Fetching libxml2-2.11.9.pkg: .......... done
[3/16] Fetching mpdecimal-4.0.0.pkg: .. done
[4/16] Fetching lua53-5.3.6_1.pkg: .... done
[5/16] Fetching libedit-3.1.20240808,1.pkg: .... done
[6/16] Fetching icu-74.2_1,1.pkg: .......... done
[7/16] Fetching postgresql17-server-17.2.pkg: .......... done
[8/16] Fetching liblz4-1.10.0,1.pkg: ... done
[9/16] Fetching llvm15-15.0.7_10.pkg: .......... done
[10/16] Fetching perl5-5.36.3_2.pkg: .......... done
[11/16] Fetching zstd-1.5.6.pkg: ...... done
[12/16] Fetching libffi-3.4.6.pkg: . done
[13/16] Fetching readline-8.2.13_1.pkg: .......... done
[14/16] Fetching gettext-runtime-0.22.5.pkg: ... done
[15/16] Fetching postgresql17-client-17.2.pkg: .......... done
[16/16] Fetching python311-3.11.10.pkg: .......... done
Checking integrity... done (0 conflicting)
[1/16] Installing indexinfo-0.3.1...
[1/16] Extracting indexinfo-0.3.1: .... done
[2/16] Installing mpdecimal-4.0.0...
[2/16] Extracting mpdecimal-4.0.0: .......... done
[3/16] Installing libedit-3.1.20240808,1...
[3/16] Extracting libedit-3.1.20240808,1: .......... done
[4/16] Installing liblz4-1.10.0,1...
[4/16] Extracting liblz4-1.10.0,1: .......... done
[5/16] Installing libffi-3.4.6...
[5/16] Extracting libffi-3.4.6: .......... done
[6/16] Installing readline-8.2.13_1...
[6/16] Extracting readline-8.2.13_1: .......... done
[7/16] Installing gettext-runtime-0.22.5...
[7/16] Extracting gettext-runtime-0.22.5: .......... done
[8/16] Installing lua53-5.3.6_1...
[8/16] Extracting lua53-5.3.6_1: .......... done
[9/16] Installing perl5-5.36.3_2...
[9/16] Extracting perl5-5.36.3_2: .......... done
[10/16] Installing zstd-1.5.6...
[10/16] Extracting zstd-1.5.6: .......... done
[11/16] Installing python311-3.11.10...
[11/16] Extracting python311-3.11.10: .......... done
[12/16] Installing libxml2-2.11.9...
[12/16] Extracting libxml2-2.11.9: .......... done
[13/16] Installing icu-74.2_1,1...
[13/16] Extracting icu-74.2_1,1: .......... done
[14/16] Installing llvm15-15.0.7_10...
[14/16] Extracting llvm15-15.0.7_10: .......... done
[15/16] Installing postgresql17-client-17.2...
[15/16] Extracting postgresql17-client-17.2: .......... done
[16/16] Installing postgresql17-server-17.2...
===> Creating groups
Creating group 'postgres' with gid '770'
===> Creating users
Creating user 'postgres' with uid '770'
===> Creating homedir(s)
=========== BACKUP YOUR DATA! =============
As always, backup your data before
upgrading. If the upgrade leads to a higher
major revision (e.g. 9.6 -> 10), a dump
and restore of all databases is
required. This is *NOT* done by the port!
See https://www.postgresql.org/docs/current/upgrading.html
===========================================
[16/16] Extracting postgresql17-server-17.2: .......... done
=====
Message from python311-3.11.10:
--
Note that some standard Python modules are provided as separate ports
as they require additional dependencies. They are available as:
py311-gdbm databases/py-gdbm@py311
py311-sqlite3 databases/py-sqlite3@py311
py311-tkinter x11-toolkits/py-tkinter@py311
=====
Message from postgresql17-client-17.2:
--
The PostgreSQL port has a collection of "side orders":
postgresql-docs
For all of the html documentation
p5-Pg
A perl5 API for client access to PostgreSQL databases.
postgresql-tcltk
If you want tcl/tk client support.
postgresql-jdbc
For Java JDBC support.
postgresql-odbc
For client access from unix applications using ODBC as access
method. Not needed to access unix PostgreSQL servers from Win32
using ODBC. See below.
ruby-postgres, py-psycopg2
For client access to PostgreSQL databases using the ruby & python
languages.
postgresql-plperl, postgresql-pltcl & postgresql-plruby
For using perl5, tcl & ruby as procedural languages.
postgresql-contrib
Lots of contributed utilities, postgresql functions and
datatypes. There you find pg_standby, pgcrypto and many other cool
things.
etc...
=====
Message from postgresql17-server-17.2:
--
For procedural languages and postgresql functions, please note that
you might have to update them when updating the server.
If you have many tables and many clients running, consider raising
kern.maxfiles using sysctl(8), or reconfigure your kernel
appropriately.
The port is set up to use autovacuum for new databases, but you might
also want to vacuum and perhaps backup your database regularly. There
is a periodic script, /usr/local/etc/periodic/daily/502.pgsql, that
you may find useful. You can use it to backup and perform vacuum on all
databases nightly. Per default, it performs `vacuum analyze'. See the
script for instructions. For autovacuum settings, please review
~postgres/data/postgresql.conf.
If you plan to access your PostgreSQL server using ODBC, please
consider running the SQL script /usr/local/share/postgresql/odbc.sql
to get the functions required for ODBC compliance.
Please note that if you use the rc script,
/usr/local/etc/rc.d/postgresql, to initialize the database, unicode
(UTF-8) will be used to store character data by default. Set
postgresql_initdb_flags or use login.conf settings described below to
alter this behaviour. See the start rc script for more info.
To set limits, environment stuff like locale and collation and other
things, you can set up a class in /etc/login.conf before initializing
the database. Add something similar to this to /etc/login.conf:
---
postgres:\
:lang=en_US.UTF-8:\
:setenv=LC_COLLATE=C:\
:tc=default:
---
and run `cap_mkdb /etc/login.conf'.
Then add 'postgresql_login_class="postgres"' to /etc/rc.conf, or
set it as the postgres user's login class in /etc/passwd.
======================================================================
To use PostgreSQL, enable it in rc.conf using
sysrc postgresql_enable=yes
To initialize the database, run
service postgresql initdb
You can then start PostgreSQL by running:
service postgresql start
For postmaster settings, see ~postgres/data/postgresql.conf
NB. FreeBSD's PostgreSQL port logs to syslog by default
See ~postgres/data/postgresql.conf for more info
NB. If you're not using a checksumming filesystem like ZFS, you might
wish to enable data checksumming. It can be enabled during
the initdb phase, by adding the "--data-checksums" flag to
the postgresql_initdb_flags rcvar. Otherwise you can enable it later by
using pg_checksums. Check the initdb(1) manpage for more info
and make sure you understand the performance implications.
[INFO] postgresql17-server installed successfully.
Updating PATH for postgresql17-server
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 1 package(s) will be affected (of 0 checked):
New packages to be INSTALLED:
postgresql17-contrib: 17.2
Number of packages to be installed: 1
The process will require 2 MiB more space.
566 KiB to be downloaded.
[1/1] Fetching postgresql17-contrib-17.2.pkg: .......... done
Checking integrity... done (0 conflicting)
[1/1] Installing postgresql17-contrib-17.2...
[1/1] Extracting postgresql17-contrib-17.2: .......... done
=====
Message from postgresql17-contrib-17.2:
--
The PostgreSQL contrib utilities have been installed. Please see
/usr/local/share/doc/postgresql/README-contrib
for more information.
[INFO] postgresql17-contrib installed successfully.
Updating PATH for postgresql17-contrib
postgresql_enable: -> YES
[SUCCESS] [INFO] Enabled postgresql service.
[INFO] /var/db/postgres/data17 not found, initializing database...
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 this locale configuration:
locale provider: libc
LC_COLLATE: C
LC_CTYPE: C.UTF-8
LC_MESSAGES: C.UTF-8
LC_MONETARY: C.UTF-8
LC_NUMERIC: C.UTF-8
LC_TIME: C.UTF-8
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/db/postgres/data17 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default "max_connections" ... 100
selecting default "shared_buffers" ... 128MB
selecting default time zone ... US/Eastern
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. You can now start the database server using:
/usr/local/bin/pg_ctl -D /var/db/postgres/data17 -l logfile start
initdb: warning: enabling "trust" authentication for local connections
initdb: hint: 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.
[INFO] Updated /var/db/postgres/data17/postgresql.conf with PostgreSQL logging settings.
pg_ctl: no server running
[INFO] Created /etc/syslog.d/postgres.conf with PostgreSQL logging configuration.
[INFO] Updated /etc/syslog.conf with PostgreSQL logging configuration.
[INFO] Reloaded PostgreSQL service.
Stopping syslogd.
Waiting for PIDS: 71188.
Starting syslogd.
[INFO] Restarted syslogd service.
pg_ctl: server is running (PID: 8213)
/usr/local/bin/postgres "-D" "/var/db/postgres/data17"
CREATE ROLE
[SUCCESS] User 4350c3ab-b186-11ef-ad68-2186cc70b5fb created successfully.
CREATE DATABASE
[SUCCESS] Database my_app_prod created successfully.
[SUCCESS] Added user 4350c3ab-b186-11ef-ad68-2186cc70b5fb@my_app_prod to pg_hba.conf.
[SUCCESS] Postgres reloaded successfully.
[SUCCESS] Username: 4350c3ab-b186-11ef-ad68-2186cc70b5fb
[SUCCESS] Password: 011de45978f77a72b1e408fa8b88b252f752ee3939d883f6
[SUCCESS] Database: my_app_prod
[WARN] Record these credentials in a secure location.
[WARN] You will not be able to retrieve the password later.
#
# Note the Username and Password for the newly created Database
#
#
# Enable Access to the DB
#
$ turn_on_user_access.sh 4350c3ab-b186-11ef-ad68-2186cc70b5fb admin@demo-pg
Uncommented existing line for user '4350c3ab-b186-11ef-ad68-2186cc70b5fb' in pg_hba.conf
#
# Open the DB with psql
#
$ psql -U 4350c3ab-b186-11ef-ad68-2186cc70b5fb -h demo-pg -d my_app_prod
Password for user 4350c3ab-b186-11ef-ad68-2186cc70b5fb:
psql (14.15 (Homebrew), server 17.2)
WARNING: psql major version 14, server major version 17.
Some psql features might not work.
Type "help" for help.
my_app_prod=> \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------+--------------------------------------+----------+---------+---------+-----------------------
my_app_prod | 4350c3ab-b186-11ef-ad68-2186cc70b5fb | UTF8 | C | C.UTF-8 |
postgres | postgres | UTF8 | C | C.UTF-8 |
template0 | postgres | UTF8 | C | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
my_app_prod=>
#
# Open the DB with TablePlus (on Mac)
#
DRIVER="postgresql"
HOST="demo-pg"
PORT="5432"
USER="4350c3ab-b186-11ef-ad68-2186cc70b5fb"
PASS="011de45978f77a72b1e408fa8b88b252f752ee3939d883f6"
DATABASE="my_app_prod"
open -a TablePlus $DRIVER://$USER:$PASS@$HOST:$PORT/$DATABASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment