How to change a database to postgresql with Symfony 2.0? - postgresql

I have a Symfony2 project under Debian. What are the steps to change my database to postgresql ?
Note: The question has been asked here, but it is with symfony 1.4, I believe.

Install postgresql under debian:
apt-get install postgresql postgresql-client
apt-get install php5-pgsql
adduser mypguser
su - postgres
psql
CREATE USER mypguser WITH PASSWORD 'mypguserpass';
CREATE DATABASE mypgdatabase;
GRANT ALL PRIVILEGES ON DATABASE mypgdatabase to mypguser;
\q
In /etc/php5/apache2/php.ini, add: (this is in fact optional)
extension=pdo.so
extension=php_pdo_pgsql.so
Change the symfony apps/config/paramters.ini file:
[parameters]
database_driver: pdo_pgsql
database_host: localhost
database_port: null
database_name: mypgdatabase
database_user: mypguser
database_password: mypguserpass
Relaod your project:
php bin/vendors update
php app/console assets:install web
php app/console doctrine:schema:create
php app/console doctrine:fixtures:load
chmod 777 -R app/cache app/logs
You're done!
References:
Symfony documentation for configuring databases
Postgresql installation under debian

Related

How to upgrade postgresql database from 10 to 12 without losing data for openproject

My OpenProject management software is installed with default postgresql 10.
Currently the postgresql DB is 12, It is having lot of new features.
I want to upgrade my Postgres DB without losing the data in the DB.
My system is ubuntu 18.04 and hosted openproject.
I searched the internet and could not find a step by step to upgrade postgresql.
Can you please guide me to install new DB and all data should be in the new DB.
thanks for your help.
A) First create a backup of all the databases for that (You can continue from B if you dont need a backup)
Log in as postgres user
sudo su postgres
Create a backup .sql file for all the data you have in all the databases
pg_dumpall > backup.sql
B) Upgrade to PostgreSQL12
update packages and install postgres 12
sudo apt-get update
sudo apt-get install postgresql-12 postgresql-server-dev-12
Stop the postgresql service
sudo systemctl stop postgresql.service
migrate the data
/usr/lib/postgresql/12/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/10/main \
--new-datadir=/var/lib/postgresql/12/main \
--old-bindir=/usr/lib/postgresql/10/bin \
--new-bindir=/usr/lib/postgresql/12/bin \
--old-options '-c config_file=/etc/postgresql/10/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf'
Switch to regular user
exit
Swap the ports the old and new postgres versions.
#change port to 5432
sudo vim /etc/postgresql/12/main/postgresql.conf
#change port to 5433
sudo vim /etc/postgresql/10/main/postgresql.conf
Start the postgresql service
sudo systemctl start postgresql.service
Log in as postgres user
sudo su postgres
Check your new postgres version
psql -c "SELECT version();"
Run the generated new cluster script
./analyze_new_cluster.sh
Return as a normal(default user) user and cleanup up the old version's mess
#uninstalls postgres packages
sudo apt-get remove postgresql-10 postgresql-server-dev-10
#removes the old postgresql directory
sudo rm -rf /etc/postgresql/10/
#login as postgres user
sudo su postgres
#delete the old cluster data
./delete_old_cluster.sh
Congrads! Your postgresql version is now upgraded, If everything works well in B, we dont have to apply the backup as we have already migrated the data from the older version to the newer version, the backup is just in case if anything goes wrong.
NOTE: Change the postgresql.conf and pg_hba.conf as per your requirement
PS: Feel free to comment your issues, suggestions or anyother modifications you would like to suggest
Backup the database
psql --version sudo -u postgres psql pg_dumpall > alldbs.sql
(this command will backup all databases from the postgresql db)
Then exit from postgres user, and:
Inside a terminal run these commands:
sudo systemctl stop postgres
sudo apt-get install -y postgresql-12 postgresql-server-dev-12 postgresql-contrib-12 libpq-dev postgresql-12-hypopg
sudo pg_dropcluster 12 main --stop
sudo pg_upgradecluster 10 main
sudo pg_dropcluster 10 main --stop
restart the postgresql service:
sudo systemctl restart postgresql
login to the postgres:
su - postgres
to check the version:
psql --version
I have done using the above steps and I could update the DBand restore all data.
Postgresql Upgrade using pg_upgrade
Step - 1 (Find the locale language and encoding of source Postgresql server)
Log in to Postgresql database as postgres user
Execute the query SHOW LC_COLLATE;
Step - 2 ( Stop the existing/source Postgresql server)
Find the running Postgresql service using $ sudo systemctl list-units | grep postgres
Stop the service $ sudo service postgresql-<source-version>.service stop
Step - 3 (Install target Postgresql server)
Configure repos and install (lot of tutorials available in google)
Step - 4 (Update the locale language and encoding)
$ /usr/pgsql-<target-version>/bin/initdb -D /var/lib/pgsql/<target-version>/data --locale=<add-your-encoding>
Step - 5 (Check the source to target upgrade has any potential issues)
Run the command as postgres user, $sudo su postgres
$ /usr/pgsql-<target-version>/bin/pg_upgrade --old-bindir /usr/pgsql-<source-version>/bin --new-bindir /usr/pgsql-<target-version>/bin --old-datadir /var/lib/pgsql/<source-version>/data --new-datadir /var/lib/pgsql/12/data --link --jobs=2 --check
If it's ok we can move to the next step, If not fix the issues before proceeding to the next step
Expected output: Clusters are compatible
Step - 6 (Upgrade the source to target Postgresql version using the link option)
link option is pretty faster than the migration
Run the command as postgres user, $sudo su postgres
$ /usr/pgsql-<target-version>/bin/pg_upgrade --old-bindir /usr/pgsql-<source-version>/bin --new-bindir /usr/pgsql-<target-version>/bin --old-datadir /var/lib/pgsql/<source-version>/data --new-datadir /var/lib/pgsql/<target-version>/data --link
Expected output: Upgrade Complete
Thanks.
ENJOY !!!

Connection between PostgreSQL and Symfony3 (Doctrine)

I'm starting a symfony project and i got a PosgreSQL database. I work on Windows 10 and Xampp
I know that xampp works with postgreSQL (i edited php.ini to make it work) because the postgre driver is shown in phpinfo and i did a php script test to connect to the DB and it works.
This is what i get in config.yml
doctrine:
dbal:
driver: pdo_pgsql
database_host: localhost
database_port: 5432
database_name: postgres
database_user: postgres
database_password: toto
charset: UTF8
However, whenever i try to use a basic console command with symfony like
php bin/console doctrine:database:create
it returns :
[Doctrine\DBAL\Exception\DriverException]
An exception occured in driver: could not find driver
[Doctrine\DBAL\Driver\PDOException]
could not find driver
[PDOException]
could not find driver
EDIT : after some hours of searching it, i figured that i had a different php folder that my console used. I've uninstalled it and added the php runned by xampp in my environment variables. And then the console used the right php and the doctrine command worked.
Problem solved.
execute php -m in your CMD if you don't see pdo_pgsql so you need to add the extension to php.ini that php execute
If you're using php7.0:
sudo apt-get install php7.0-pgsql
For ondrej/php Ubuntu repository with php7.1:
sudo apt-get install php7.1-pgsql
Same repository, but for php5.6:
sudo apt-get install php5.6-pgsql

PostgreSQL ,Postgis and Pgrouting compatible versions

I am trying to work with PostgreSql 9.3, Postgis 2.1.3 and Pgrouting 2.0.0 versions in Ubuntu (VPS). I have installed PostgreSql 9.3,but when i run CREATE EXTENSION postgis SCHEMA public VERSION "2.0.0"; in PgAdmin3 i get ERROR: could not stat file "/usr/share/postgresql/9.3/extension/postgis--2.1.3.sql": No such file or directory and same error with Pgrouting 2.0.0.Are these versions compatibles running in that system?,in localhost work fine.
sudo apt-get update
sudo apt-get install postgresql-9.3 postgresql-9.3-postgis-2.1 postgresql-9.3-postgis-scripts postgresql-contrib-9.3 postgresql-client-common postgresql-common postgresql-server-dev-9.3 postgresql-client-9.3
This should install everything and start the server. Read the following man pages. to create a user and a database.
man createuser
man createdb
and this will install postgis into database 'mydatabase' for user 'user':
psql -U user mydatabase -c "create extension postgis"

No passwd entry for user 'postgres' error

I'm trying to setup Postgres on Ubuntu 14.04 without success. After running
sudo apt-get install libpq-dev python-dev
and
sudo apt-get install postgresql postgresql-contrib
I've managed to install the packages successfully. However upon executing sudo su - postgres, I'm receiving the following error:
No passwd entry for user 'postgres'
It seems like the installation did not create the user postgres as it is not in the /etc/passwd file. Reinstalling the package does not resolve the issue. How can I add the required postgres user?
It sounds like you want the PostgreSQL server:
apt-get install postgresql-server
Also, you never need to use sudo su - postgres. Just:
sudo -u postgres -i
to get an interactive shell, or
sudo -u postgres psql
(or whatever) to run a command.
I've solved the issue by purging and re-installing the packages, i.e.
sudo apt-get --purge remove postgresql*
sudo apt-get install postgresql postgresql-contrib

cannot find adminpack.sql in postgresql 9.1

I'm trying to get it installed and use in ubuntu. Was following this page to install the additional instrumentation packages.
Install Server Instrumentation for Postgresql 8.4
To install Server Instrumentation for Postgresql 8.4 you may use the command line and type:
sudo apt-get install postgresql-contrib
Run the adminpack.sql script, simply type:
sudo -u postgres psql < /usr/share/postgresql/8.4/contrib/adminpack.sql
But I couldn't find adminpack.sql in 9.1 version.
In PostgreSQL 9.1 and later, extensions can be installed via the CREATE EXTENSION command:
sudo -u postgres psql
CREATE EXTENSION "adminpack";
Try locate adminpack. But first, run updatedb to make sure the locate database is up to date.
sudo updatedb
locate adminpack
The output is:
/usr/lib/postgresql/9.1/lib/adminpack.so
/usr/share/postgresql/9.1/extension/adminpack--1.0.sql
/usr/share/postgresql/9.1/extension/adminpack.control
Looks like this is old and comments are closed, so I'm opening another answer.
ldalonzo's answer is correct once you have all the components, but I found I also had to run:
sudo apt-get install postgresql-contrib-9.1
Looks like the bare postgresql package doesn't include the additional modules.
Once you install postgresql-contrib using below command
sudo apt-get install postgresql-contrib
Adminpack is located in the below path
/usr/share/postgresql/9.1/extension/adminpack--1.0.sql
To load the adminpack use the below command
sudo su postgres -c psql < /usr/share/postgresql/9.1/extension/adminpack--1.0.sql
Then
sudo /etc/init.d/postgresql restart