HomeVideos

I replaced my entire tech stack with Postgres...

Now Playing

I replaced my entire tech stack with Postgres...

Transcript

270 segments

0:00

modern web development sucks we have

0:01

amazing tools that solve every problem

0:03

and technically it's never been easier

0:05

to build a billion dooll failed app idea

0:07

the problem is that by the time you

0:09

deploy it you end up paying 20 different

0:11

ycb startups to use their fancy shovels

0:13

you think you need an in-memory cache

0:15

database to handle your low to five

0:16

users you need a cron service you need

0:18

authentication performance monitoring

0:20

analytics serverless functions and the

0:22

list goes on it's no coincidence that

0:24

America's homeless population started to

0:25

rise immediately after the release of

0:27

nextjs in 2016 but what if I told you

0:30

you could throw all of these shovels in

0:31

the incinerator and use nothing but

0:33

postgressql yes the relational database

0:36

to achieve 90% of your web development

0:38

needs in today's video we'll look at 11

0:40

useful unorthodox and just plain weird

0:43

ways to use postgress by the end of it

0:45

we will have built a full stack

0:46

application using nothing but an open

0:48

source database but first of all what

0:50

makes postgress special and why not use

0:52

something like MySQL or SQL light well

0:54

out of the box postgress provides

0:55

Advanced Data types like binary Json

0:58

arrays key value stores and even

1:00

geometric types to Define shapes but

1:02

more importantly it's extensible and you

1:04

can even create your own custom data

1:05

types over the years this has led to a

1:07

massive ecosystem of extensions where

1:09

developers have given it all kinds of

1:11

crazy new features it's just like

1:12

modding a game where you take something

1:14

that sucks like Civilization 7 and

1:16

install a bunch of mods to make it

1:17

playable now before we get into the

1:18

first example I first want to warn you

1:20

that just because you can do something

1:22

in postgress doesn't mean you should

1:23

before you go and cancel all your shovel

1:25

subscriptions don't forget to think

1:26

critically and make sure you use the

1:28

right tool for the job one of the great

1:29

debates among web developers is SQL

1:31

versus nosql and one of the key selling

1:33

points for no SQL is that you can work

1:35

with unstructured data like objects with

1:37

Dynamic data that might be different for

1:39

each record you can't do that in SQL

1:41

because every table has a fixed schema

1:43

well actually that's not true because

1:45

postgress supports binary Json in this

1:47

query you'll notice how we create a

1:48

table with a Json B type and then insert

1:50

some values into it as raw Json strings

1:53

so pretty cool and believe it or not we

1:55

can actually query this data now we can

1:56

use a select statement followed by a

1:58

wear clause and then this syntax to

2:00

filter and access Fields inside this

2:02

unstructured data that feature is built

2:04

right into postgress but now we're going

2:06

to implement some really crazy stuff

2:07

thanks to extensions first though you

2:09

might be wondering where do I get a

2:11

postgress database it's free and open

2:13

source to host locally but an even

2:15

easier option is neon the sponsor of

2:17

today's video they may not condone all

2:19

the crazy stuff I'm showing you in this

2:20

video but they provide postgress on a

2:22

serverless platform that makes it really

2:24

easy to Kickstart and scale your

2:25

projects not only do they have an

2:27

awesome free tier but there's also

2:28

developer friendly guides for all the

2:30

biggest Frameworks and OMS developers

2:32

already love postgress and neon makes it

2:34

even easier to work with by providing

2:36

features like branching which allows you

2:37

to easily create different data

2:39

workflows for development testing and

2:41

production and will automatically scale

2:43

up with zero effort as your app grows

2:45

give neon a try for free right now using

2:46

the link below to create a postgress

2:48

database then take your connection

2:49

details to a VSS code extension like SQL

2:52

tools and connect to it locally and now

2:54

we're ready to do some really crazy

2:55

stuff with extensions developers only

2:57

want one thing and it's disgusting a

2:59

Chron job which is code that runs on a

3:01

schedule now normally you would have to

3:03

edit the cron tab file on Linux to

3:04

achieve this or pay a sass to do it for

3:06

you but postgress will actually give you

3:08

a Cron job for free if we install the PG

3:10

cron extension we can then write a SQL

3:12

statement that runs a KRON schedule give

3:14

it a name Define the schedule and then

3:16

it will run the SQL statement on that

3:17

schedule like you might want to delete

3:19

or aggregate a bunch of data on a daily

3:21

basis that's cool but another thing you

3:23

think you might need but probably don't

3:24

is an in-memory cache database like

3:26

reddis or mcash well in postgress you

3:28

can create your own po man's redus by

3:30

using an unlog table as a cache this

3:32

prevents right ahead logging where

3:34

changes to the database are written to a

3:36

log file before being applied to data

3:38

files and that provides the D for

3:39

durability in asset and that means if

3:41

your server crashes unexpectedly

3:43

committed transactions will not be lost

3:45

however you don't really need that for a

3:46

cache and disabling it provides better

3:48

performance then you can update the

3:49

postgress config to store it in a shared

3:51

buffer which will keep the data highly

3:53

available in Ram and then summon the

3:55

autov vacu demon to avoid bloat and

3:57

maybe throw in PG KRON to automatically

3:59

delete any entries with an expired time

4:01

to live but this wouldn't be a proper

4:02

fireship video if I didn't mention Ai

4:05

and if you're building an AI app that

4:06

uses rag stack you might consider paying

4:08

for a vector database but what you could

4:10

do instead is just install the PG Vector

4:12

extension it provides a vector data type

4:14

that allows you to store

4:15

multi-dimensional data then you can do

4:17

things like make a query for nearest

4:19

neighbors based on L2 distance and you

4:21

can even take things a step further with

4:22

the pgai extension not only does it

4:24

handle Vector embeddings but it allows

4:26

you to load your own data set and

4:28

vectorize it all entirely in SQL code

4:30

but speaking of vectors A more

4:32

traditional feature you might need is a

4:33

full Tech search engine where users type

4:35

into a search box and get results even

4:37

if they have typos the postgress

4:39

actually has built-in support for this

4:40

with the TS Vector type it'll take a

4:42

string like this and break it up into a

4:44

bunch of smaller parts then we can

4:45

create a generalized inverted index for

4:47

better performance which gives us the

4:49

ability to then Rank and query the most

4:51

likely results from the data using

4:52

double app which is the tech search

4:54

operator and now you don't have to pay

4:56

for expensive tools like algolia or

4:58

elastic search if you're sick of writing

4:59

SQL code though one thing you might do

5:01

instead is use graphql the magic PG

5:03

graphql extension can transform your

5:05

database into a graphql API and that

5:08

makes it incredibly easy for any

5:09

programming language to understand in

5:11

query your database just add the

5:13

extension and then start writing graphql

5:15

resolvers directly in your SQL code and

5:17

best of all no additional servers

5:19

libraries or paid SAS middlemen are

5:21

required but what if you're building a

5:22

real-time application using tools like

5:24

Firebase or superbase that's a difficult

5:26

feature to roll out on your own but a

5:28

library called electric SQL fixes this

5:30

it's not a PG extension but rather a

5:32

sync layer which would sit in between

5:34

your database and your front end code to

5:36

ensure that the end user always has

5:37

fresh data and that means you don't have

5:39

to mess around with websockets or write

5:40

a bunch of broken code to automatically

5:42

fetch data when it changes and as an

5:44

added bonus it works great with neon but

5:46

another thing developers like to brag

5:47

about is rolling their own off usually

5:49

by writing some crappy JavaScript

5:51

instead of paying a SAS to do that for

5:53

them but real men roll their own off

5:55

entirely in postgress and that's

5:56

possible when you combine a couple of

5:58

extensions like PG crypto crypto and PG

6:00

Json web token like if we have a table

6:02

with a username and password we can use

6:04

crypto to Hash the password and also

6:06

generate Assa that ensures that when our

6:08

database gets hacked the attacker won't

6:10

have access to the plain text password

6:12

and now to verify a user's login

6:14

credentials we can write a simple query

6:16

that matches the username to the hashed

6:18

password with the Crypt function but the

6:20

other thing we'll need is a way to

6:21

validate user sessions on the server and

6:23

one way to do that is with Json web

6:25

tokens and PG JWT makes it really easy

6:28

to cryptographically sign a token on the

6:30

server and now that we have that we can

6:32

Implement roow level security where the

6:33

end user can only read and write data

6:35

that's actually owned by their username

6:37

this is a built-in feature in postgress

6:39

where we create policies that Define

6:41

rules about how our data can be accessed

6:43

and in this case we'll verify the

6:44

current user's token before executing

6:46

any queries and that means selecting

6:48

everything from the horse's table will

6:49

only give us the hes owned by that user

6:52

at this point we have a pretty complex

6:53

application and we should start

6:54

collecting some analytics data instead

6:56

of Google analytics or another paid SAS

6:58

an option you'll want to check out is PG

7:00

moon cake basically it turns postgress

7:02

into a Time series database and it's

7:04

extremely fast and efficient because it

7:06

adds support for column store tables

7:08

with duck DB execution and all this data

7:10

can be dropped into a cloud storage

7:12

bucket and or visualized with other open

7:14

source tools like grafana that's cool

7:15

and all but we'll never actually get any

7:17

analytics data if we don't make our data

7:19

available on the internet to achieve

7:21

that you might think you need to build a

7:22

restful API with some programming

7:24

language from scratch but another option

7:26

is post rest which automatically turns

7:28

your database into a uccessful API after

7:30

you get it set up you'll be able to go

7:32

into the browser then navigate to Local

7:33

Host followed by a table name and it

7:35

will automatically query your postgress

7:37

data and return it in Json format on top

7:39

of that you can do all kinds of other

7:40

stuff like filtering pagination

7:42

authentication and so on and now the

7:44

final step to build a true full stack

7:46

application is to store some HTML CSS

7:48

and JavaScript in the database in fact

7:50

there are even some crazies out there

7:52

getting react server components to wrun

7:53

in post grass and you really can't get

7:55

your data any closer to the UI than that

7:57

congratulations you're now officially a

7:59

certified postgress Rison big shout out

8:01

to Neon for reviewing and sponsoring

8:03

this video and although they told me

8:05

they don't approve of all the crazy

8:06

stuff I showed you in this video I'm

8:08

just going to go ahead and upload it

8:09

anyway thanks for watching and I will

8:11

see you in the next one

Interactive Summary

This video explores how PostgreSQL, through its powerful extension ecosystem and built-in features, can replace many third-party services—such as cron job providers, caching solutions, and authentication services—in modern web development. It demonstrates how to leverage PostgreSQL for diverse tasks including AI vector search, real-time data synchronization, and API creation, ultimately allowing developers to build complex, full-stack applications using primarily an open-source database.

Suggested questions

3 ready-made prompts