Starting in week 3 of Ruby and Rails, we'll be using a tool called Postgres for SQL database management. Even though we won't be using this tool for a few weeks, it's important to install it now so you'll have ample time to troubleshoot if there are any issues.
Postgres requires a fair amount of configuration to run correctly on Windows. You might find it helpful to use this Stack Overflow answer to guide you.
If you have Windows, begin by registering and installing Postgres from its main site. Make sure to install version 12.3, for Windows. Use the default values for the setup, and make sure to keep note of the password you use for Postgres. For the purposes of this lesson, we will assume the password
Add Postgres to your PATH by running the following:
$ echo 'export PATH="/c/Program Files/PostgreSQL/12/bin:$PATH"'
Open the application
pgAdmin 4, which should be located in the directory
C:\Program Files\PostgreSQL\12\pgAdmin 4\bin. On the left site of the page you should see the "Browser" panel. In this panel, there should be a tree including
PostgreSQL 12. Right-click on
Login/Group Roles, under
PostgreSQL 12, and select
Create > Login/Group Role.... The
Name for this role should be the same as the name of your Operating System username -- so, whatever user you would see at
C:\Users. In the
Privileges tab, set all privileges for your account to
Finally, to start the Postgres server, you'll need to run:
$ pg_ctl -D "C:\Program Files\PostgreSQL\12\data" start
Once you've completed the set up instructions, ensure you can access Postgres server via the command line. Open GitBash and run this command:
This should open the Postgres command prompt. You can type
\q to exit from this prompt.
If you've already set up Ruby according to the earlier lessons, you have Homebrew installed. Homebrew makes it really easy to install Postgres. To install the version of Postgres used at Epicodus, run the following command:
$ brew install [email protected]
Note: While you may choose to install the latest version of Postgres on your home environment, do not ever try to change the version on Epicodus computers. Remember that in a professional environment, you will be expected to adapt to the environment used at your workplace, not the other way around!
After it finishes installing, you'll need to configure your computer a bit. First, you need to tell Postgres where to find the database cluster where your databases will be stored:
For bash users:
echo "export PGDATA=/usr/local/var/postgres" >> ~/.bash_profile
For zsh users:
echo "export PGDATA=/usr/local/var/[email protected]" >> ~/.zshrc
Next, enter this command to help some programs find Postgres more easily:
For bash users:
echo "export PGHOST=/tmp" >> ~/.bash_profile
For zsh users:
echo "export PGHOST=/tmp" >> ~/.zshrc
To load these configuration changes, run
To start the Postgres server, simply run:
You'll have to leave that window open while you need the server. To stop the server, press Ctrl + C (not Cmd + C).
If you want Postgres to boot at startup and run in the background, run:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
And to start it now (since it won't boot automatically until you restart your computer), run:
The computers at Epicodus are configured to have Postgres launch at startup, so you don't need to start it manually in class.
If you get an error after running
postgres that states something similar to
postgres: could not access directory "/usr/local/var/postgres": No such file or directory, double check the name of the
postgres file, by entering the following commands:
$ cd ~ $ cd ../.. $ cd usr/local/var $ ls
The terminal output should include a folder for postgres. It may be named
[email protected]. Whatever it is, the path we set for
PGDATA in the shell's config file needs to be updated. Enter the following commands to open your shell's config file in VS Code:
$ code ~/.bash_profile
$ code ~/.zshrc
Once opened, update the path you have for
PGDATA. This may change from:
export PGDATA=/usr/local/var/[email protected]
To prepare for upcoming lessons, create a default database with your computer's username:
And you're done!
Lesson 7 of 8
Last updated October 13, 2021