Heroku Postgres in PHP: Quick Start

"Heroku Postgres is a managed SQL database service provided directly by Heroku (https://devcenter.heroku.com/articles/heroku-postgresql).

In this post, I am going to use Heroku Postgres in PHP.


create app

% mkdir heroku && cd heroku

% touch index.php

% git init

% git add .

% git commit -am 'init'

% heroku create

% heroku apps


add database

Confirm "No add-ons" yet.

% heroku addons

Add database as a free plan.

% heroku addons:create heroku-postgresql:hobby-dev
% heroku pg:info
=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           0/20
PG Version:            13.2
Created:               2021-05-08 00:45 UTC
Data Size:             7.9 MB
Tables:                0
Rows:                  0/10000 (In compliance) - refreshing
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-aerodynamic-50817

Note: with the free plan, the number of records is limited to max 10,000.


connect manually

Install psql (& postgres itself as needed).

% brew install postgres

Access database.

% heroku pg:psql

Create a table and insert a test record.

create table users (user_id serial primary key);

insert into users (username) values ('foo');

exit

You can see that Rows has been incremented.

% heroku pg:info
=== DATABASE_URL
Plan:                  Hobby-dev
Status:                Available
Connections:           0/20
PG Version:            13.2
Created:               2021-05-08 00:45 UTC
Data Size:             8.2 MB
Tables:                1
Rows:                 
1/10000 (In compliance)
Fork/Follow:           Unsupported
Rollback:              Unsupported
Continuous Protection: Off
Add-on:                postgresql-aerodynamic-50817


connect from php

"As part of the provisioning process, a DATABASE_URL config var is added to your app’s configuration" (https://devcenter.heroku.com/articles/heroku-postgresql)


Replace index.php with the following.

% vim index.php

Deploy the application.

% git commit -am 'connect db'

% git push heroku master

Check if the inserted record is displayed.

% heroku open


cleanup

% heroku addons:destroy heroku-postgresql:hobby-dev

% heroku apps:destroy


Comments

Popular posts from this blog

Selenide: Quick Start

Minikube Installation for M1 Mac

Server Testing Tools: Serverspec, InSpec, Testinfra, Goss