Skip to content

Code Snippets

We use this page to share and store code snippets we find useful and reusable.

Example Ruby snippet
s = "Ruby syntax highlighting"
print s
def order_lookup_params
params
.require(:order_lookup)
.permit(:tote_id, :order_number)
.each_value { |value| value.try(:strip!) }
end
Example JS snippet
const s = "JavaScript syntax highlighting";
console.log('s', s);
Example CSS snippet
.btn {
border: none; /* Remove borders */
color: white; /* Add a text color */
padding: 14px 28px; /* Add some padding */
cursor: pointer; /* Add a pointer cursor on mouse-over */
}
Creating a Postgres Cluster Locally

Create a Primary - Follower clusrter setup on Mac using the Postgres.app.

The reason for choosing Postgres.app is because it’s very easy to run several servers in parallel, each on a separate port.

  1. Install Postgres.app

  2. Create the Primary instance with defaults

  3. Configure the Primary for Replication: Edit “/Users/…/Library/Application Support/Postgres/var-16/postgresql.conf” with the following changes:

wal_level = replica
archive_mode = on
synchronous_commit = remote_apply
synchronous_standby_names = '*'
wal_log_hints = on
  1. Start the Primary instance from the Postgres.app UI

  2. Create a physical Replication Slot on the Master named slot1. Connect to the master database using psql and execute the command:

SELECT * FROM pg_create_physical_replication_slot('slot1');
  1. Create an empty folder which will be used by the Follower db as the data folder.
Terminal window
mkdir path/to/follower/folder

Just for reference, I’ve used “/Users/sergiu/Library/Application Support/Postgres/var-16-follower”.

  1. cd into the Postgres.app binaries for the Primary folder:
Terminal window
cd /Applications/Postgres.app/Contents/Versions/16/bin
  1. Use pg_basebackup to create a copy of the Primary data folder on the follower.
Terminal window
./pg_basebackup --pgdata="path/to/follower/folder" --write-recovery-conf --slot="slot1"

This will connect to the Primary (that’s why --port=5432 is not given) and start copying all the data into the new folder.

  1. Change the rights for the Follower db data folder:
Terminal window
sudo chmod 0700 "/path/to/follower/folder"
  1. Start the Follower from Postgres.app UI.

  2. Create the kids_passport_development db on the Primary and import the db dump file into it:

Terminal window
psql kids_passport_development < development_application_db_export.sql
  1. Optional: if you’re connecting to these databases from a Docker app, you will have to update the host in database.yml to the following:
host: host.docker.internal