• Blog
  • Docs
  • Careers
  • Get Support
  • Contact Sales
DigitalOcean
  • Featured AI Products

    Compute

    Build, deploy, and scale cloud compute resources

    Containers and Images

    Safely store and manage containers and backups

    Managed Databases

    Fully managed resources running popular database engines

    Management and Dev Tools

    Control infrastructure and gather insights

    Networking

    Secure and control traffic to apps

    Security

    Help protect your account and resources with these security features

    Storage

    Store and access any amount of data reliably in the cloud

    Browse all products

  • AI/ML

    CMS

    Data and IoT

    Developer Tools

    Gaming and Media

    Hosting

    Security and Networking

    Startups and SMBs

    Web and App Platforms

    See all solutions

  • Community

    Documentation

    Developer Tools

    Get Involved

    Utilities and Help

  • Become a Partner

    Marketplace

  • Pricing
  • Log in
  • Sign up
  • Log in
  • Sign up

Company

  • About
  • Leadership
  • Blog
  • Careers
  • Customers
  • Partners
  • Referral Program
  • Affiliate Program
  • Press
  • Legal
  • Privacy Policy
  • Security
  • Investor Relations

Products

  • Knowledge Bases
  • GPU Droplets
  • Bare Metal GPUs
  • Inference Engine
  • Data & Learning
  • Evaluations
  • Model Library
  • Droplets
  • Kubernetes
  • Functions
  • App Platform
  • Load Balancers
  • Managed Databases
  • Spaces
  • Block Storage
  • Network File Storage
  • API
  • Uptime
  • Cloud Security Posture Management (CSPM)
  • Identity and Access Management (IAM)
  • Cloudways
  • View all Products

Resources

  • Community Tutorials
  • Community Q&A
  • CSS-Tricks
  • Write for DOnations
  • Currents Research
  • DigitalOcean Startups
  • Wavemakers Program
  • Compass Council
  • Open Source
  • Newsletter Signup
  • Marketplace
  • Pricing
  • Pricing Calculator
  • Documentation
  • Release Notes
  • Code of Conduct
  • Shop Swag

Solutions

  • AI Training GPU
  • GPU Inference
  • VPS Hosting
  • Website Hosting
  • VPN
  • Docker Hosting
  • Node.js Hosting
  • Web Mobile Apps
  • WordPress Hosting
  • Virtual Machines
  • View all Solutions

Contact

  • Support
  • Sales
  • Report Abuse
  • System Status
  • Share your ideas

Company

  • About
  • Leadership
  • Blog
  • Careers
  • Customers
  • Partners
  • Referral Program
  • Affiliate Program
  • Press
  • Legal
  • Privacy Policy
  • Security
  • Investor Relations

Products

  • Knowledge Bases
  • GPU Droplets
  • Bare Metal GPUs
  • Inference Engine
  • Data & Learning
  • Evaluations
  • Model Library
  • Droplets
  • Kubernetes
  • Functions
  • App Platform
  • Load Balancers
  • Managed Databases
  • Spaces
  • Block Storage
  • Network File Storage
  • API
  • Uptime
  • Cloud Security Posture Management (CSPM)
  • Identity and Access Management (IAM)
  • Cloudways
  • View all Products

Resources

  • Community Tutorials
  • Community Q&A
  • CSS-Tricks
  • Write for DOnations
  • Currents Research
  • DigitalOcean Startups
  • Wavemakers Program
  • Compass Council
  • Open Source
  • Newsletter Signup
  • Marketplace
  • Pricing
  • Pricing Calculator
  • Documentation
  • Release Notes
  • Code of Conduct
  • Shop Swag

Solutions

  • AI Training GPU
  • GPU Inference
  • VPS Hosting
  • Website Hosting
  • VPN
  • Docker Hosting
  • Node.js Hosting
  • Web Mobile Apps
  • WordPress Hosting
  • Virtual Machines
  • View all Solutions

Contact

  • Support
  • Sales
  • Report Abuse
  • System Status
  • Share your ideas
© 2026 DigitalOcean, LLC.Sitemap.
Cloud education

Some Postgres Best Practices

author

By Kenneth Reitz

  • Published: March 4, 2019
  • 3 min read
<- Back to blog home

Over my many years of serving as a Developer Relations Advocate, I’ve met and spoken with thousands of developers all over the world about their database configurations, and many have shared with me their lessons learned.

To celebrate the launch of our new managed database product, I’d like to share a few key takeaways you might apply to your new managed PostgreSQL databases.

Use Connection Strings & Environment Variables

The Twelve Factors, a set of best practices for building web applications for the cloud, is very clear about one thing: never store your database credentials in your codebase! Your code should be considered both proprietary and of inherent value, while at the same time being considered inherently insecure. Your data is what’s truly valuable.

So, to provide trusted credentials to trusted code, we recommend exposing the connection string of your Postgres database as an environment variable, named DATABASE_URL. Every common web framework has support for this (e.g., with Django, see dj-database-url).

Rotate Credentials Regularly

It’s a good idea to rotate your database credentials periodically, across your organization. However, during special times, like when offboarding a member of the team, it’s important to rotate the credentials to the Managed Databases and Spaces they had access to.

All it takes is one stray .env file from a couple of years ago to get into the wrong hands, and then your entire infrastructure could be instantly compromised. I recommend having organization-wide quarterly “credential rotation” days, with hard deadlines. This (likely) tedious process will be forced to become streamlined as your team continues to rotate the credentials month after month, encouraging automation. It’s best for everyone.

Use BIGINT or UUID for Primary Keys

A big mistake in a lot of applications is using `INT` instead of `BIGINT` for primary keys (every Django application does this by default, for example).

When you use INT instead of BIGINT, eventually, one day, the value in your database exceeds the “storage capacity” of an INT, and a BIGINT must be migrated to. The migration time to convert a table from INT to BIGINT usually takes around 4 hours, once the problem has been located.

BIGINT is much more appropriate as a default PK, and is not that much more expensive to store or index.

I personally use UUIDs in all of my databases:

-- Enable pgcrypto for UUID support. CREATE EXTENSION pgcrypto; – Table: notes CREATE TABLE notes ( uuid UUID DEFAULT gen_random_uuid(), body text NOT NULL, byline text, CONSTRAINT notes_pk PRIMARY KEY (uuid) );

Learn more about uuid4 / pgcrypto in Postgres.

Use Connection Pooling

When connecting to a high-throughput Postgres database server, it’s considered best practice to configure your clients to use PgBouncer, a lightweight connection pooler for PostgreSQL, instead of connecting to the database server directly.

Connection pooling has many performance advantages, and will make the query performance characteristics of your database much more deterministic.

However, here at DigitalOcean, we take care of that for you! So, there’s no need to run your own instance of PgBouncer when using DigitalOcean Postgres, as it’s already provided and pre-tuned out of the box. Simplicity at scale.

Further Reading

Here are some further resources for upping your Postgres game:

  • Postgres Guide
  • Managed Databases Connection Pools and PostgreSQL Benchmarking

Happy hacking!

Kenneth Reitz (@kennethreitz)

Developer Relations Advocate

About the author

Kenneth Reitz
Kenneth Reitz
Author

Share

  • Cloud Education

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.
Sign up

Related Articles

Introducing langchain-gradient: Seamless LangChain Integration with DigitalOcean Gradient™ AI Platform
Cloud education

Introducing langchain-gradient: Seamless LangChain Integration with DigitalOcean Gradient™ AI Platform

Narasimha Badrinath

  • August 19, 2025
  • 2 min read

Read more

Agentic Cloud: Reinventing the Cloud with AI Agents
Cloud education

Agentic Cloud: Reinventing the Cloud with AI Agents

Bratin Saha, Chief Product & Technology Officer

  • May 19, 2025
  • 5 min read

Read more

How to optimize your cloud architecture for business growth
Cloud education

How to optimize your cloud architecture for business growth

Anantha Ramachandran
  • May 9, 2025
  • 5 min read

Read more