SaltyCrane: machttps://www.saltycrane.com/blog/2019-01-11T13:01:39-08:00New Mac setup notes 2019
2019-01-11T13:01:39-08:00https://www.saltycrane.com/blog/2019/01/new-mac-setup-notes-2019/<h4>install Homebrew</h4>
<pre class="console">
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
</pre>
<h4><code>brew cask install</code> stuff</h4>
<pre class="console">
$ brew cask install iterm2
$ brew cask install emacs
$ brew cask install kdiff3
</pre>
<h4><code>brew install</code> stuff</h4>
<pre class="console">
$ brew install bat
$ brew install exa
$ brew install fzf
$ brew install glances
$ brew install gnupg
$ brew install icdiff
$ brew install markdown
$ brew install node@10
$ brew install postgresql
$ brew install python
$ brew install rbenv
$ brew install readline
$ brew install ripgrep
$ brew install screen
$ brew install tldr
</pre>
<h4>copy SSH keys</h4>
<h4>set up homedir</h4>
<pre class="console">
$ cd /tmp
$ git clone git@github.com:saltycrane/homedir.git
$ rsync -avz homedir/ ~/
</pre>
<h4>set up spacemacs</h4>
<p>
See
<a href="https://github.com/saltycrane/.spacemacs.d#usage">
https://github.com/saltycrane/.spacemacs.d#usage</a
>
</p>
<h4>install stuff via websites</h4>
<ul>
<li><a href="https://pqrs.org/osx/karabiner/">Karabiner Elements</a></li>
<li><a href="https://www.hammerspoon.org/">Hammerspoon</a></li>
<li><a href="https://docs.docker.com/docker-for-mac/install/">Docker</a></li>
<li><a href="https://www.dropbox.com/">Dropbox</a></li>
<li>XCode (via App Store)</li>
</ul>
<h4>misc stuff</h4>
<ul>
<li>
fix the Meta/Alt/Option key in iTerm
<blockquote>
iTerm2 > Preferences > Profiles > Keys > Change Option key to "Esc+"
</blockquote>
</li>
<li>
set a directory for screenshots
<pre class="console">
$ defaults write com.apple.screencapture location /Users/eliot/Pictures </pre
>
</li>
<li>
allow gpg decrypting in emacs
<pre class="console">
$ brew install pinentry-mac
$ echo "pinentry-program /usr/local/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf</pre
>
</li>
<li>
python2 and pycrypto setup
<pre class="console">
$ brew install python2
$ pip install virtualenv
$ brew install gmp # for pycrypto</pre
>
</li>
</ul>
<h4 id="see-also">see also</h4>
<ul>
<li><a href="/blog/2015/10/os-x-notes/">macOS notes</a></li>
<li>
<a href="https://github.com/saltycrane/homedir"
>https://github.com/saltycrane/homedir</a
>
</li>
<li>
<a href="https://github.com/saltycrane/.spacemacs.d"
>https://github.com/saltycrane/.spacemacs.d</a
>
</li>
<li>
<a href="https://twitter.com/AdamRackis/status/1084526704108978177">
https://twitter.com/AdamRackis/status/1084526704108978177</a
>
</li>
<li>
<a href="https://github.com/benjie/dotfiles/blob/master/brew">
https://github.com/benjie/dotfiles/blob/master/brew
</a>
</li>
<li>
<a href="https://github.com/kentcdodds/dotfiles/blob/master/.macos"
>https://github.com/kentcdodds/dotfiles/blob/master/.macos
</a>
</li>
</ul>
How to run PostgreSQL in Docker on Mac (for local development)
2019-01-09T22:47:12-08:00https://www.saltycrane.com/blog/2019/01/how-run-postgresql-docker-mac-local-development/<p>
These are my notes for running Postgres in a Docker container for use with a
local Django or Rails development server running on the host machine (not in
Docker). Running in Docker allows keeping my database environment isolated
from the rest of my system and allows running multiple versions and instances.
(I previously had a problem where Homebrew upgraded Postgres when I didn't
expect it to and my existing database became incompatible. Admittedly, I
didn't know Homebrew well, but it was frustrating.) Disadvantages of Docker
are it's another layer of abstraction to learn and interact with. We use
Docker extensively at work, so from a mental overhead point of view, it's
something I wanted to learn anyways. Currently I use the Homebrew Postgres for
work, and Postgres in Docker for personal projects. I also wrote some
<a href="/blog/2017/07/postgres-mac-os-homebrew-notes/"
>notes on Postgres and Homebrew here</a
>.
</p>
<h4 id="install-docker">Install Docker</h4>
<p>
Install Docker for Mac:
<a href="https://docs.docker.com/docker-for-mac/install/"
>https://docs.docker.com/docker-for-mac/install/</a
>.
</p>
<p>
Alternatively, you can install Docker using Homebrew:
<code>brew install homebrew/cask/docker</code>
</p>
<h4 id="using-single-docker-command">
OPTION 1: Run Postgres using a single Docker command
</h4>
<b>Run a postgres container</b>
<ul>
<li>
uses the
<a href="https://hub.docker.com/_/postgres/"
>official docker postgres 13 image</a
>
</li>
<li>
uses a <a href="https://docs.docker.com/storage/volumes/">named volume</a>,
<code>my_dbdata</code>, to store postgres data
</li>
<li>
exposes port 54320 to the host using
<a
href="https://github.com/docker/cli/blob/master/docs/reference/commandline/run.md#publish-or-expose-port--p---expose"
><code>-p</code></a
>
</li>
<li>sets the container name to <code>my_postgres</code></li>
<li>uses the <code>-d</code> flag to run in the background</li>
<li>
sets the postgres superuser password to <code>"my_password"</code> using
<a
href="https://github.com/docker/cli/blob/master/docs/reference/commandline/run.md#set-environment-variables--e---env---env-file"
><code>-e</code></a
>
and the
<a
href="https://github.com/docker-library/docs/blob/master/postgres/README.md#postgres_password"
><code>POSTGRES_PASSWORD</code> environment variable</a
>.
</li>
</ul>
<pre class="console">
$ docker run -d --name my_postgres -v my_dbdata:/var/lib/postgresql/data -p 54320:5432 -e POSTGRES_PASSWORD=my_password postgres:13
</pre>
<h4 id="using-docker-compose">OPTION 2: Run Postgres using Docker Compose</h4>
<b>Create a <code>docker-compose.yml</code> file</b>
<pre class="console">
$ mkdir /tmp/myproject
$ cd /tmp/myproject
</pre>
<p>Create a new file <code>docker-compose.yml</code>:</p>
<pre>
version: "3"
services:
db:
image: "postgres:13"
container_name: "my_postgres"
environment:
POSTGRES_PASSWORD: "my_password"
ports:
- "54320:5432"
volumes:
- my_dbdata:/var/lib/postgresql/data
volumes:
my_dbdata:
</pre>
<ul>
<li>uses docker compose file version 3</li>
<li>
sets up a service named <code>"db"</code> (this name can be used with
<code>docker-compose</code> commands)
</li>
<li>
uses the
<a href="https://hub.docker.com/_/postgres/"
><code>postgres:13</code> image from hub.docker.com</a
>
using the
<a
href="https://docs.docker.com/compose/compose-file/compose-file-v3/#image"
><code>image</code> key</a
>
</li>
<li>
creates a container named <code>"my_postgres"</code> using the
<a
href="https://docs.docker.com/compose/compose-file/compose-file-v3/#container_name"
><code>container_name</code> key</a
>
</li>
<li>
sets the postgres superuser password to <code>"my_password"</code> using the
<a
href="https://docs.docker.com/compose/compose-file/compose-file-v3/#environment"
><code>environment</code> key</a
>
and the
<a
href="https://github.com/docker-library/docs/blob/master/postgres/README.md#postgres_password"
><code>POSTGRES_PASSWORD</code> environment variable</a
>
</li>
<li>
connects port 5432 inside Docker as port 54320 on the host machine using the
<a
href="https://docs.docker.com/compose/compose-file/compose-file-v3/#ports"
><code>ports</code> key</a
>
</li>
<li>
uses a named volume, <code>"my_dbdata"</code>, for storing the database data
using the
<a
href="https://docs.docker.com/compose/compose-file/compose-file-v3/#volumes"
><code>volumes</code> key</a
>. Even if the container and image are deleted, the volume will remain
unless explicitly deleted using <code>docker volume rm</code>.
</li>
<li>
for more information, see the
<a href="https://docs.docker.com/compose/compose-file/"
>Docker Compose file reference</a
>
</li>
</ul>
<b>Start Postgres</b>
<p>
Pull the postgres image from hub.docker.com, create a container named
"my_postgres", and start it in the background:
</p>
<pre class="console">
$ docker-compose up -d
</pre>
<h4 id="see-working">See that it's working</h4>
<p>See the logs:</p>
<pre class="console">
$ docker logs -f my_postgres
</pre>
<p>Try running psql:</p>
<pre class="console">
$ docker exec -it my_postgres psql -U postgres
</pre>
<p>hit CTRL+D to exit</p>
<p>
For other commands such as starting, stopping, listing or deleting, see my
<a href="/cheat-sheets/docker/">Docker cheat sheet</a>.
</p>
<h4 id="create-database">Create a database</h4>
<pre class="console">
$ docker exec -it my_postgres psql -U postgres -c "create database my_database"
</pre>
<h4 id="connect-using-python">
Connect using Python and <code>psycopg2</code>
</h4>
<pre class="console">
$ python3 -m venv myenv
$ source myenv/bin/activate
$ pip install psycopg2-binary
</pre>
<p>Create a new file named <code>myscript.py</code></p>
<pre class="python">
import psycopg2
conn = psycopg2.connect(
host='localhost',
port=54320,
dbname='my_database',
user='postgres',
password='my_password',
)
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS test (id serial PRIMARY KEY, num integer, data varchar);")
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abcdef"))
cur.execute("SELECT * FROM test;")
result = cur.fetchone()
print(result)
conn.commit()
cur.close()
conn.close()
</pre>
<p>Run it</p>
<pre class="console">
$ python myscript.py
(1, 100, 'abcdef')
</pre>
<h4 id="errors">Errors</h4>
<ul>
<li>
<pre><code>docker: Error response from daemon: Conflict. The container name "/my_postgres" is already in use by container "b27594a414db369ec4876a07021c9ea738a55b3bc0a3ad5117158367131b99a2". You have to remove (or rename) that container to be able to reuse that name.</code></pre>
<p>If you get the above error, you can remove the container by running:</p>
<pre class="console">$ docker rm my_postgres</pre>
</li>
<li>
<pre><code>Error response from daemon: You cannot remove a running container 7e94d205b6f4ef40ff885987f11e825e94eddbcd061481e591e07c87ed7cf86e. Stop the container before attempting removal or force remove</code></pre>
<p>If you get the above error, you can stop the container by running:</p>
<pre class="console">$ docker stop my_postgres</pre>
</li>
</ul>
<h4 id="see-also">See also</h4>
<ul>
<li>
<a href="/blog/2017/07/postgres-mac-os-homebrew-notes/">
My Postgres Homebrew notes
</a>
</li>
<li>
<a href="https://www.saltycrane.com/blog/2017/08/docker-cheat-sheet/"
>My Docker cheat sheet</a
>
</li>
<li>
<a href="https://hub.docker.com/_/postgres/"
>postgres image on Docker Hub</a
>
</li>
<li>
<a href="https://docs.docker.com/engine/reference/commandline/run/">
<code>docker run</code> command reference
</a>
</li>
<li>
<a href="https://docs.docker.com/compose/compose-file/">
Docker Compose file reference
</a>
</li>
<li>
<a href="https://docs.docker.com/storage/volumes/"
>Documentation on using volumes</a
>
</li>
</ul>
postgres on Mac OS (homebrew) notes
2017-07-20T16:25:33-07:00https://www.saltycrane.com/blog/2017/07/postgres-mac-os-homebrew-notes/<p>
Here are my notes on running postgresql on macOS Mojave installed with Homebrew.
I also wrote some
<a href="/blog/2019/01/how-run-postgresql-docker-mac-local-development/">
notes on running postgres in Docker here</a
>.
</p>
<h4>install postgres</h4>
<pre>
$ brew install postgresql
</pre>
<h4>start postgres</h4>
<pre>
$ brew services start postgresql
</pre>
<h4>restart postgres</h4>
<pre>
$ brew services restart postgresql
</pre>
<h4>postgres version</h4>
<pre>
$ postgres --version
postgres (PostgreSQL) 11.1
</pre>
<h4>check postgres is running (option 1)</h4>
<pre>
$ brew services list
Name Status User Plist
postgresql started eliot /Users/eliot/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
</pre>
<h4>check postgres is running (option 2)</h4>
<pre>
$ ps -ef | grep postgres
502 629 1 0 10Jan19 ?? 0:12.39 /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
502 715 629 0 10Jan19 ?? 0:00.29 postgres: checkpointer
502 716 629 0 10Jan19 ?? 0:02.39 postgres: background writer
502 717 629 0 10Jan19 ?? 0:02.32 postgres: walwriter
502 718 629 0 10Jan19 ?? 0:09.99 postgres: autovacuum launcher
502 719 629 0 10Jan19 ?? 0:45.17 postgres: stats collector
502 720 629 0 10Jan19 ?? 0:00.22 postgres: logical replication launcher
502 73227 19287 0 8:43PM ttys003 0:00.00 grep postgres
</pre>
<h4>postgres log file</h4>
<pre>
/usr/local/var/log/postgres.log
</pre>
<h4>postgres data directory</h4>
<pre>
/usr/local/var/postgres
</pre>
<h4>psql commands</h4>
Start psql:
<pre>
$ psql postgres
</pre>
Help with psql commands:
<pre>
postgres=# \?
</pre>
List databases:
<pre>
postgres=# \l
</pre>
Connect to a database:
<pre>
postgres=# \c mydatabase
</pre>
List tables:
<pre>
mydatabase=# \d
</pre>
How to lock the screen in Mac OS X with an external keyboard
2016-01-07T08:46:02-08:00https://www.saltycrane.com/blog/2016/01/how-lock-screen-mac-os-x-external-keyboard/<h4 id="mojave">macOS Mojave</h4>
<p>With the laptop keyboard, <kbd>Cmd</kbd> + <kbd>Ctrl</kbd> + <kbd>Q</kbd></p>
<h4 id="yosemite">OS X Yosemite</h4>
<p>
With the laptop keyboard, <kbd>Ctrl</kbd> + <kbd>Shift</kbd> +
<kbd>Power</kbd> is used to lock the screen on my 2015 MacBook Pro w/ OS X
Yosemite. To lock the screen with an external PC keyboard, use
<a href="https://pqrs.org/osx/karabiner/"> Karabiner</a> to map the
Pause/Break key to Power.
</p>
macOS notes
2015-10-06T22:52:35-07:00https://www.saltycrane.com/blog/2015/10/os-x-notes/<h4 id="take-screenshot">How to take a screenshot</h4>
<p><kbd>command</kbd>+<kbd>shift</kbd>+<kbd>4</kbd> click and drag to select an area</p>
<p><kbd>command</kbd>+<kbd>shift</kbd>+<kbd>4</kbd> <kbd>space</kbd> click to select a window</p>
<p><kbd>command</kbd>+<kbd>shift</kbd>+<kbd>4</kbd> <kbd>space</kbd> <kbd>option</kbd>+click to select a window (saves w/o window shadow)</p>
<p>From <a href="https://support.apple.com/kb/PH18798?locale=en_US">https://support.apple.com/kb/PH18798?locale=en_US</a></p>
<h4 id="screenshot-save-location">How to change the location for saving screenshots</h4>
<pre class="console">$ defaults write com.apple.screencapture location ~/Pictures
$ killall SystemUIServer </pre>
<p>From <a href="http://mac-how-to.wonderhowto.com/how-to/change-default-save-location-screenshots-mac-os-x-for-cleaner-desktop-0160154/">http://mac-how-to.wonderhowto.com/how-to/change-default-save-location-screenshots-mac-os-x-for-cleaner-desktop-0160154/</a></p>
<h4 id="enter-path-finder">How to type in a path in Finder or a File Dialog box</h4>
<p><kbd>command</kbd>+<kbd>shift</kbd>+<kbd>G</kbd></p>
<p>From <a href="http://apple.stackexchange.com/questions/43291/how-to-change-path-in-finder-by-entering-the-path-manually">http://apple.stackexchange.com/questions/43291/how-to-change-path-in-finder-by-entering-the-path-manually</a></p>
<h4 id="emoji-picker">How to select emoji</h4>
<p><kbd>command</kbd>+<kbd>control</kbd>+<kbd>space</kbd></p>
<p>From <a href="https://twitter.com/james_k_nelson/status/1090508067504975873">https://twitter.com/james_k_nelson/status/1090508067504975873</a>
Switching to OS X and front end development
2015-09-10T08:26:36-07:00https://www.saltycrane.com/blog/2015/09/switching-osx-and-front-end-development/<p>After 7 years, I've yielded to the Dark Side and switched from Ubuntu to OS X on my work laptop.
I've also switched from Python and back end web development to JavaScript and front end development.
The former is mostly to support the latter.
</p>
<p>Linux is rare<sup>1</sup>, especially among front end developers,
and I want to make it easy to collaborate as I learn new things.
I've had problems working with Photoshop files in GIMP and I couldn't run
the iOS simulator. <a href="https://news.ycombinator.com/item?id=10812214">
Issues with Linux device drivers</a> don't help.
</p>
<p>I'm choosing front end development because I want to code closer to the end user.<sup>2</sup>
In small part like Ian Bicking
<a href="http://www.ianbicking.org/blog/2014/02/saying-goodbye-to-python.html">
wrote last year</a>,
I feel unexcited about back end development and really excited about
JavaScript and front end development. I'm excited about
ES 2015 and
React and
React Native and
CSS transitions.<sup>3</sup>
I'm even coming around to Node.js.
JavaScript is <a href="https://www.destroyallsoftware.com/talks/wat">uglier</a>
than Python, but it's getting
<a href="https://medium.com/google-developers/exploring-es7-decorators-76ecb65fb841">
better</a>
and there are things
<a href="https://glyph.twistedmatrix.com/2015/09/software-you-can-use.html">
Python can't do</a> that
<a href="http://steve-yegge.blogspot.com/2007/02/next-big-language.html">
JavaScript can</a>.<sup>4</sup> If only beauty mattered, maybe I'd use Scheme.<sup>5</sup>
</p>
<p>I'm sure I will hate OS X at first, but hopefully it will be good in the
long run. If anyone can recommend a tiling window manager like
<a href="http://www.qtile.org/">Qtile</a> for OS X, please let me know.
</p>
<p><em>(I will continue using Emacs because <a href="http://emacsrocks.com/">Emacs rocks!</a> <sup>6</sup>)</em>
</p>
<hr>
<small>
<ol>
<li>I think I was the last person at my company running Linux.</li>
<li>I've been trying to do front end work for years now, but
I finally got a sensible chance to switch as my company is
changing it's technology stack from Python to Ruby and
Backbone/Angular to React.</li>
<li><em>Update 2016-01-04:</em> Here are even <a href="http://www.2ality.com/2016/01/web-technologies-2015.html">
more exciting web technologies</a>: Electron, progressive web apps, and WebAssembly.</li>
<li><em>Update 2016-01-01:</em> I found James Hague had <a href="http://prog21.dadgum.com/203.html">
similar thoughts</a> on Python and JavaScript.</li>
<li>Speaking of functional languages and JavaScript, <a href="http://elm-lang.org/">Elm</a> sounds pretty cool.</li>
<li><em>Update 2016-01-26:</em> <a href="/blog/2015/12/switching-emacs-vim-actually-spacemacs/">Or will I?</a></li>
</ol>
</small>