The Answer by Craig Ringer is more important than this one. But I want to explain how tools can help you with this matter.
Database migration tools
We now have database migration tools to help with the chore of updating your:
- Database structure
- Adding/dropping/modifying schema, tables, columns, indexes, and such.
- a.k.a. DDL
- Data contents
…as needed when your app(s) evolve.
You have a choice of several tools, including LiquiBase and Flyway.
Flyway
I have used Flyway successfully in my own work. So a brief description follows, though I urge you to do a bit of research to see which tools fit your needs and style.
Flyway is built in Java, but can be used in non-Java shops as well. All of the Flyway commands are wrapped in command-line executables, so you can use them on the console or from scripts. If you are a Java house, you may choose to call the commands from within Java using objects. And for specialized needs you may choose to write Java classes to do some heavy lifting that might not be possible in straight SQL or SQL-superset (PL/pgSQL etc.) database programming.
Flyway automatically installs a table into your database, to store its own metadata. Flyway looks through designated folders for SQL scripts you have written. The specific naming conventions prescribed by Flyway control the order of execution. Each time you run Flyway, the tool examines all your scripts, looking up each one in that metadata to see if it was already run in this particular database. If not yet run, Flyway runs it and logs the results. If the script was previously run, Flyway uses a checksum to be sure the script has not been edited, otherwise flagging a potential problem.
Using a database migration tool such as Flyway is best done from the beginning of the project. But you can add Flyway to an existing database, see the documentation. Or consider recreating the database using a set of SQL scripts organized for Flyway, if that is feasible for your situation. I like the ability to recreate a new database from scratch entirely through Flyway – handy for development and testing.
Speaking of testing, you can easily add or drop SQL scripts (and optionally Java classes) to do work for testing purposes. Such purposes might include loading a known set of data. Or you might want to set up certain conditions for recreating a bug scenario.
Flyway is quite easy to work with, given its smart defaults and simple design. But you can also customize its behavior, overriding defaults, for special needs.
Flyway works well with Postgres as in operates through JDBC, either its own bundled copy of JDBC drivers or through yours. For non-Java shops, the use of the JDBC driver is transparent to you. Few features of JDBC are actually used, as Flyway does little more than pass your SQL scripts to the database. Besides creating and updating its own metadata table, Flyway is not really touching your database – you are manipulating the database yourself, through those scripts you wrote. Metaphorically, think of Flyway as the postal carrier, while you are the letters’ author.
Flyway is freemium, with a Community Edition that is open-source and free-of-cost. This edition has all the classic features that may Flyway so popular. These features may be all you ever need. Additional specialized features and technical support are available in further editions at a cost. See this feature grid. The Flyway project and company were recently acquired by Redgate Software, with Flyway creator Axel Fontaine joining.