| Title | Reason | Reference |
|---|---|---|
Set up PostgreSQL on Google Compute Engine Virtual Machines for Remote Access |
Wasted around 3 hours why my postgres installation was not accessible outside the machine, although all setup was correct. finally found it. |
- Install PostgreSQL on a Compute Engine instance.
- Configure PostgreSQL for remote access.
- Configure a Google Cloud firewall to open a port.
- Connect to PostgreSQL from a remote computer.
You'll need a Google Cloud project. You can use an existing project or create a new project.
For the purposes of this tutorial, the default machine type works fine, so you don't need to change the default setting. In production, you need to decide how much computing power is required for your application. In general, database systems tend to be more constrained by I/O bottlenecks and hard disk speed than by CPU capabilities.
Most Linux distributions have some version of PostgreSQL integrated with their package managers. For this tutorial, you use Ubuntu 20.04 LTS (Focal Fossa) because it includes PostgreSQL 12.5, which has some helpful tools that aren't available in earlier versions.
- In the Cloud Console, go to the VM instances page.
- Click Create instance.
- In the Name field, enter
postgres-tutorial. - In the Boot disk section, click Change.
- In the Boot disk window, perform the following steps in the Public images tab:
- In the Operating system menu, select Ubuntu.
- In the Version menu, select Ubuntu 20.04 LTS.
- In the Boot disk type menu, select Standard persistent disk.
- Click Select.
- In the Firewall section, expand Management, security, disks, networking, sole tenancy, and then expand Networking.
- In the Network tags field, enter
postgres-tutorial. - Click Create to create the instance.
It will take a few moments to create your new instance.
Stay on the VM instances page for the next step.
To set up PostgreSQL, you must install it and set up a user.
Follow these steps to install PostgreSQL on your Compute Engine instance.
-
Login to the virtual machine using ssh or any other method you want.
-
Update the packages. In the SSH terminal, enter the following command:
sudo apt update -
Install PostgreSQL, including the PSQL client and server instrumentation:
sudo apt -y install postgresql postgresql-client postgresql-contrib
PostgreSQL created a default user, named postgres, during installation. This
user doesn't yet have a password, so you need to set one.
-
Run
psqlas userpostgres, instead ofroot, accessing the database namedpostgres:sudo -u postgres psql postgresYou should see the PSQL command prompt, which looks like this:
postgres=# -
Enter the following command to set the password:
\password postgres -
When prompted, enter and confirm the password you've chosen.
-
Install the
adminpackextension to enable the server instrumentation that you installed earlier. The console printsCREATE EXTENSIONwhen successful.CREATE EXTENSION adminpack; -
Enter
\qto exit PSQL.
To connect to your Postgres database, you need to change a configuration file and open a port in the firewall on Google Cloud.
By default, Postgres doesn't allow remote connections. To change this setting,
you can change the file named
pg_hba.conf.
Caution: On production systems, or any system that has an internet connection, use strong authentication methods and restrict traffic to only those users and IP addresses that you want to connect to each database.
-
In the SSH terminal window, edit
pg_hba.conf. This tutorial uses thenanoeditor, but you can substitute your favorite editor. For PostgreSQL version 12, you can enter:sudo nano /etc/postgresql/12/main/pg_hba.conf -
Navigate to ip4.me to get the IPv4 address of your local computer.
You need this IP address in an upcoming steps.
-
Scroll down to the bottom of the file and add the following lines:
# IPv4 remote connections for the tutorial: host all all [YOUR_IPV4_ADDRESS]/32 md5Replace
[YOUR_IPV4_ADDRESS]with the address of your local computer. Note that the CIDR suffix/32is used for a single address, which is what you're providing in this tutorial.You can add as many as ip address you want, just add another line.
-
Save the file and exit the editor. In nano, press
Control+x, pressy, and then use theEnterkey to accept the prompts to save the file. Note that nano might not clear the console screen properly, so if you have trouble reading the text in the console after closing nano, enterclearto clear the screen.
-
In the terminal, enter the following command:
sudo nano /etc/postgresql/12/main/postgresql.conf -
Scroll down to the line that begins with
#listen_addresses = 'localhost'. -
Delete the
#character to uncomment the line. -
Replace
localhostwith*:listen_addresses = '*'The
'*'setting enables Postgres to listen on all IP addresses. This is a commonly used setting. When you set the IP address inhba.confin the previous step, you restricted access to the database to only your computer. -
Save the file and exit the editor.
-
Restart the database service. In the terminal, enter:
sudo service postgresql restart
PostgreSQL accepts remote connections on port 5432. Follow these steps to add a firewall rule that enables traffic on this port.
-
In the Cloud Console, navigate to the Create a firewall rule page.
-
In the Name field, enter
postgres-tutorial. -
In the Network field, leave the network as default.
-
In the Direction of traffic field, select Ingress.
-
In the Action on match field, select Allow.
-
In the Targets menu, select Specified Target tags.
-
In the Targets tags field, enter the network tag (
postgres-tutorial) that you used for the instance. -
In the Source filter menu, select IPv4 ranges.
-
In the Source IPv4 ranges field, enter the same IP address that you used in
hba.conf.This is the IP address of your local computer. Remember to include the
/32suffix, for example:1.2.3.4/32. -
In Specified protocols and ports, check tcp, and enter
5432for the value. -
Click Create.
Firewall rules are a global resource, so you'll only need to create this rule once for all instances.
Now you can connect to your PostgreSQL database from your computer. This tutorial uses pgAdmin, which is a popular client application for working with Postgres databases.
-
Install pgAdmin on your local computer.
-
(macOS only) Move pgAdmin to a location from which you can run it:
- Right-click the pgAdmin icon and copy it.
- Open the macOS
Applicationfolder, and paste pgAdmin into this folder.
-
Start pgAdmin by clicking its icon in the
Applicationfolder. -
Add the server. In pgAdmin4, you can click the first icon on the left side of the toolbar. Alternatively, click File > Add server.
-
In the New Server Registration window, in the Name field, enter the following:
Postgres tutorial -
On the VM instances page, find the external IP address of your Compute Engine instance in the External IP column.
-
In pgAdmin, in the Connection tab, in the Hostname/address field, enter the external IP address of your Compute Engine instance.
Note: Enter only the address as it appears in the Cloud Console; don't add any protocol specifiers, such as
http://or other characters. -
In the Port field, enter
5432. -
In the Password field, enter the password that you set previously for the user named
postgres. -
Click Save to close the window.
You should now be connected to your PostgreSQL database that is hosted on your Compute Engine instance. You can use pgAdmin to browse and modify the database and other settings. PgAdmin also includes a PSQL console that you can use to administer the database remotely.