I'm starting a project using a PostgreSQL database. I know that the mysql_* functions are deprecated and it is best practices to use PDO with MySQL databases, but what about PostgreSQL? Are the pg_* functions deprecated or on their way to being deprecated? Is PDO or pg_* the preferred method of talking to postgres? Is one faster or more secure than the other?
-
1This question isn't exactly a duplicate of the linked question as it is about whether pq_* will be deprecated like msql_* functions. Neither the question nor the answers here are addressed in the linked question/answers. – thor Oct 31 '14 at 19:36
4 Answers
No, the pg_*
set of functions isn't deprecated. But using PDO offers more freedom to change the database type once. This freedom is achieved by having the same function / methods to access a couple of different databases. Database specific work will be handled by the PDO driver in use.
The mysql
extensions has been marked as deprecated in favour of mysqli
not in favour of PDO
. This is because the original, old mysql
extensions didn't offered features like prepared statements or calling stored procedures. The developers decided to create a new, improved, mysql extension and replaced the old one completely.

- 152,036
- 28
- 249
- 266
ext/mysql is deprecated because it's ancient and has a much better successor: ext/mysqli.
ext/pgsql is not deprecated and has no successor, it's good as is, you can use it as the native Postgres database interface.
PDO is a unified interface to abstract many different database drivers under the same API. It is also a viable interface to use. The native pgsql interface may or may not offer some specific features specific to Postgres which PDO does not support in its abstracted interface. The pgsql API is "closer to the metal" if you will, which may be an advantage if you need it. However, unless you know of some specific thing you need, you probably won't find much of a difference.
Personally I like PDO. If you're unsure, I'd try writing some simple test scripts and decide for one or the other based on which feels more comfortable and which documentation you understand better. That is, unless you have some other deciding factors, such as the flexibility to more easily switch to other databases with the abstraction PDO offers.

- 510,633
- 85
- 743
- 889
Unless you plan to switch databases in the future, it really depends on what are you more comfortable with - the OOP approach or the old-school function-calling approach. The security of your app doesn't depend on PDO/pg_* functions but on you using safe programming approaches such as prepared statements (http://be.php.net/manual/en/pdo.prepare.php) which is available in both styles.
You could also consider using an abstraction layer such as ADOdb which supports recordset caching (http://phplens.com/adodb/caching.of.recordsets.html) and other nifty features.

- 1,895
- 1
- 13
- 20
If you know that you are going to stick with using Postgresql as the RDBMS for this project then you are best served by the Postgresql extension rather than PDO.
PDO is essentially an abstraction layer - anecdotally you'll get better performace if you utilise the native pg_ functions. If possible though, do a few benchmarks to get a better idea.
Recommended Reading: PDO vs pg_* functions.