Installing Postgres for Phoenix server
Dear Reader,
I was getting started with Phoenix server, which depends on a Postgres database, when the setup took more time than the get down-to-it. Here is a more linear setup to save time.
- Install Postgres using adf
- Create database for current user
- Change user password
- Install phppgadmin
- Create a new phppgadmin user for Phoenix
- Update the config/dev.exs file with database credentials
1. Install Postgres using asdf
If asdf is not already installed go to the github page and follow the instructions. Then run the following commands to install the Postgres plugin and set it globally.
$ asdf plugin-add postgres github.com/smashedtoatoms/asdf-postgres.git $ asdf install postgres 9.6.1 $ asdf global postgres 9.6.1
2. Create a database for the current user
As of writting, Postgres automatically creates a user and associates a database with them. But that database is not always created which leads to errors like these.
$ psql: FATAL: database "user" does not exist
To fix this run:
$ createdb your_username
From this stackoverflow answer it looks like the “user” defaults to the current user which is correct in our case.
3. Change user password
Now that a database has been created, a password has to be set. Run the command below and follow the prompt.
$ postgres=# \password
4. Install phppgadmin
Your installation steps will naturally depend on your platform so why don’t we ask the professor instead.
- Create a new phppgadmin user for Phoenix server
In Postgres, roles are users so I use the terms interchangeably. Although it’s not necessary to create a new role for development, I do this to avoid giving an application super user privileges. In my setup, my application user is called “developer” and it never expires, inherits from my admin user with login, create db and create roles privileges.
In your browser go to localhost/phppgadmin and login with your user from step 3. Select the roles tab and then create a new role.
6. Update the config/dev.exs file with database credentials
After all that, we can safely run.
$ mix ecto.create