TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Securing a Postgres Database

312 pointsby bryanmikaelianabout 4 years ago

17 comments

throwaway13337about 4 years ago
&gt;By default, PostgreSQL listens on a TCP port 5432.<p>This post seems to outright state that by default postgres is listening to everyone via TCP for connection.<p>This is not true.<p>Unless you edit pg_ident.conf, your postgres install will not listen for connections outside of on localhost. So, while it&#x27;s correct to say that it listens to TCP port 5432, there is a very narrow limit to whom it&#x27;s listening for. Namely, the same machine.<p>Postgres is pretty secure by default. It doesn&#x27;t allow external connections. It also requires a username and password to connect with permissions on the database you&#x27;re connecting to.<p>Compare that to something like redis. They at least used to listen for connections external by default and not even have a password to connect. I can imagine it&#x27;s still very easy to find all kinds of interesting stuff snooping around on port 6379.
评论 #26676531 未加载
评论 #26677303 未加载
评论 #26678094 未加载
评论 #26676145 未加载
评论 #26678025 未加载
评论 #26675773 未加载
评论 #26676707 未加载
paulryanrogersabout 4 years ago
&gt; Unfortunately, this sort of air-gapped setup is not something PostgreSQL supports out-of-the-box.<p>Is a reverse tunnel really air gapped?<p>I thought AG meant one had to physically touch the device and transfer using devices without any network capability, such as a flash drive?
评论 #26677860 未加载
评论 #26676535 未加载
评论 #26676383 未加载
评论 #26676444 未加载
greatjack613about 4 years ago
I personally secure my postgres instances by putting them in a silod vpc without internet access. I then limit incoming connections to only be allowed from the separate vpc containing the application server<p>I then use a bastion host when I need to access ssh on the instance. The bastion host remains off and inaccessible except for when I need to perform maintenance.<p>The advantages of this is that there is no &quot;always-open&quot; access to the instance.<p>Not sure why the author does not advocate this.
评论 #26675323 未加载
评论 #26675222 未加载
评论 #26675694 未加载
评论 #26675454 未加载
评论 #26676062 未加载
评论 #26675659 未加载
评论 #26679181 未加载
评论 #26675203 未加载
评论 #26675212 未加载
fmajidabout 4 years ago
A good overview, but the most secure way is to not give any table-level access privileges and only allow access using SECURITY DEFINER stored procedures (the PG equivalent of the setuid bit). For instance if you have a table of users with login and hashed salted password, you could implement a check_password() procedure. If the app account is compromised, it would not have access to the password hashes or even be able to enumerate users.
评论 #26676154 未加载
评论 #26676593 未加载
评论 #26679980 未加载
tylerjlabout 4 years ago
It feels weird to me that this blog post would suggest acquiring Let&#x27;s Encrypt certificates for certificate encryption. While it&#x27;s great for something public facing that needs your CA installed by default, creating certificates for things that you probably don&#x27;t want being public - like your database backend - just makes it more discoverable. For example, certificate transparency logs mean that anybody can see what certificates Let&#x27;s Encrypt has handed over to you. Example: <a href="https:&#x2F;&#x2F;crt.sh&#x2F;?q=ycombinator.com" rel="nofollow">https:&#x2F;&#x2F;crt.sh&#x2F;?q=ycombinator.com</a><p>Of course, this is a security through obscurity type of approach, and you&#x27;d want to secure your database whether or not somebody knew where it was running. But there&#x27;s a difference between somebody seeing that you&#x27;ve just created `staging-psql.foo.com` that you might still be configuring, and the passive background noise of internet port scanning that&#x27;s a little less targeted.
评论 #26677979 未加载
101008about 4 years ago
This is surely a silly question (and probably a lot of devops will think I am an idiot), but what do you recommend to secure a PostgreSQL that is accesed by different IPs? We use Azure PostgreSQL database, which I guess they take care of most of the security, but they allow us to set up firewall rules, as only allowing connectins by established IPs.<p>My (our) problem is that we use a lof ot AWS lambda functions that read and write to the database, and they always execute from different (dynamic) IPs, so what is the best solution in this case?
评论 #26676776 未加载
traceroute66about 4 years ago
Seems the old adage of &quot;beware of blog posts bearing gifts&quot; strikes again.<p>For blog post masquerading as a how-to on securing Postgres, the fact the article only fleetingly mentions the word &quot;function&quot; once is not cool.<p>One of the biggest things you can do for Postgres (or any database for that matter) is enforce the use of stored procedures (&quot;functions&quot; in PG-speak) rather than direct SQL queries.<p>SQL Injection attacks are common as muck. Stored procedures are a quick and easy way to mitigate them.<p>Stored procedures also have the added bonus of allowing the DBA to remain in control and ensure a higher quality of SQL query rather than upstream devs sending all manner of unoptimised SQL queries.
SomaticPirateabout 4 years ago
I see quite a few posts discussing running Postgres in a separate VPC or network. We currently run our Postgres databases within Kubernetes and leverage network policies to ensure that only application pods can access the database.<p>The application pods ingest the db credentials from Vault. The biggest concern we have today is automating credential rotation.<p>Curious if anyone else has a similar setup or thoughts on ours?
评论 #26677146 未加载
megousabout 4 years ago
Removing the public schema and not allowing users to connect to all databases by default is also a good idea.
perlgeekabout 4 years ago
What exactly is the security benefit of a reverse tunnel, in comparison to just listening on a port?<p>Usually DB servers are &quot;always on&quot; and always accept connections, so the reverse tunnel also needs to be always up.<p>Regarding row-level security: that sounds quite awesome, but in the form it&#x27;s described in the article, very limited in the number of use cases.<p>Quite often you have a web app that talks to the DB and that uses a service account. So as with, with row-level security you can just allow or disallow things to the service account, not to the user logged into the web application.<p>Is there a way to drop from the service account into a less-privileged role inside a transaction or so?
评论 #26676034 未加载
评论 #26676201 未加载
indemnityabout 4 years ago
Possibly stupid questions.<p>(1) When is operating your own PostgreSQL instance desirable?<p>(2) Isn’t this equivalent to running an RDS instance in its own VPC with public access turned off and only allowing comms with app VPC?
评论 #26680141 未加载
评论 #26676493 未加载
评论 #26676526 未加载
评论 #26682182 未加载
评论 #26676388 未加载
评论 #26680112 未加载
stephen123about 4 years ago
Teleport looks cool. We have a pretty elaborate home grown bastion host setup. Seems like an area that should have more competition. Especially after looking at teleports pricing.
评论 #26676977 未加载
Dowwieabout 4 years ago
Check out using Hashicorp Vault for dynamic postgres credentials and securely obtaining them during system init
dathinababout 4 years ago
Is there any reason <i>against</i> using wireguard to prevent any external connections to postgres <i>if</i> both the postgres clients and server are under your full control (e.g. only your server connect to postgres)?
评论 #26679330 未加载
e2e4about 4 years ago
<a href="https:&#x2F;&#x2F;supabase.io&#x2F;docs" rel="nofollow">https:&#x2F;&#x2F;supabase.io&#x2F;docs</a> is a good example of adding a nice security later (and more) on top of postgres
johnx123-upabout 4 years ago
<a href="https:&#x2F;&#x2F;archive.ph&#x2F;FsOHN" rel="nofollow">https:&#x2F;&#x2F;archive.ph&#x2F;FsOHN</a> (as the article is now removed)
nodamageabout 4 years ago
&gt; log_statement = all<p>Is this something people really do? Wouldn&#x27;t these logs be enormous and&#x2F;or potentially leak unwanted information?
评论 #26680966 未加载