Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a website with a PostgreSQL database?

Tags:

php

postgresql

I'm doing a group project and we're creating an online game. We're about half way done and now it's time to implement a database to store our records/data and make the website go live on the internet.

I'm just confused on how PSQL works exactly. My understanding is that PSQL needs to be running on some server in order to access it. For previous assignments, I downloaded Postgres for my Mac and ran it on localhost. The PHP code was something along the lines of:

$dbconn = pg_connect("host=localhost port=5432 dbname=mydbname");

So, if we intend to use PSQL, where would the server be? Do one of us have to host the server? Can we use some sort of free online server? How do we connect to that server with PHP?

In summary, I have two main questions:

  1. How do we make our code go live on the internet for free? (It's just a temporary website and will only be up for a few weeks at most)

  2. How can we all access a shared PSQL database?

Sorry for the noob questions, I just got started with web development and am still learning.

like image 502
Ryan Smith Avatar asked Oct 29 '22 10:10

Ryan Smith


1 Answers

So, if we intend to use PSQL, where would the server be? Do one of us have to host the server? Can we use some sort of free online server? How do we connect to that server with PHP?

PostGreSQL is going to have to run on some machine visible to anyone who needs to access it. If only your web server (i.e., the machine running PHP and your website) needs to talk to the PGSQL, then PGSQL can be installed on your web server. This is a very common configuration.

The server might also run on the LAN where your web server is running or it might be running on an entirely different network on a different continent. The most important thing is that any machine which must connect directly to the database can actually connect to it. If you're building a website, this means you have a web server. Your web server will need to connect to the PGSQL server. The second most important thing is that your web server and the PGSQL server should share a very fast connection for the sake of performance and efficiency.

It's probably most common for your web server to also host the database. On an ubuntu machine, installing a PostGreSQL server is as easy as running a few commands. A quick search yields many examples like this one.

How do we make our code go live on the internet for free? (It's just a temporary website and will only be up for a few weeks at most)

I don't know anyone who is in the habit of offering free web hosting or DBMS services. You could ask a friend. Or put an ad on craigslist or something. Or if you are tech-savvy (it doesn't sound like you are) then you could configure a high-end router at your home to use Dynamic DNS to point some domain at a machine running at your house.

How can we all access a shared PSQL database?

I have no experience with Heroku, but you might sniff around there. PostGreSQL's website also maintains a list of hosting companies. Amazon offers RDS instances running PGSQL. Digital Ocean has a variety of tutorials and how-tos on dealing with PostGres. You could probably fire up a 'droplet' server for super cheap and install it yourself without too much effort.

like image 126
S. Imp Avatar answered Nov 15 '22 06:11

S. Imp