<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Postgres | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/postgres/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Mon, 31 Oct 2022 09:12:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Doctrine migrations and Postgis</title>
		<link>https://nerdpress.org/2022/10/31/doctrine-migrations-and-postgis/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Mon, 31 Oct 2022 09:12:46 +0000</pubDate>
				<category><![CDATA[Doctrine ORM]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[Postgis]]></category>
		<category><![CDATA[Postgres]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3184</guid>

					<description><![CDATA[<p>Using Postgres with the Postgis extension to integrate GeoData / GIS functionality in your project is not natively supported by Doctrine and Doctrine migrations. First you have to add the extension to Postgres, even if you use the Postgis docker image like postgis/postgis:14-3.3-alpine. So add this SQL statement to the up method of your first &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2022/10/31/doctrine-migrations-and-postgis/" class="more-link">Continue reading<span class="screen-reader-text"> "Doctrine migrations and Postgis"</span></a></p>
The post <a href="https://nerdpress.org/2022/10/31/doctrine-migrations-and-postgis/">Doctrine migrations and Postgis</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Using Postgres with the Postgis extension to integrate GeoData / GIS functionality in your project is not natively supported by Doctrine and Doctrine migrations.</p>



<p>First you have to add the extension to Postgres, even if you use the Postgis docker image like <code>postgis/postgis:14-3.3-alpine</code>.</p>



<p>So add this SQL statement to the up method of your first migration:<br /><code>$this->addSql('CREATE EXTENSION IF NOT EXISTS postgis;');</code><br /></p>



<p>and the DROP statement for the extension to the down method:<br /><code>$this->addSql('DROP EXTENSION postgis;');</code></p>



<p>Now, when using Doctrine with Postgres and Postgis extension, migrations still behave a bit odd and try to remove Sequences created by Postgis, because Doctrine migrations does not take Postgis extension&#8217; s built-in Sequences into account.</p>



<span id="more-3184"></span>



<p>So you will find this statements in your first migration, which is basically wrong because &#8211;<em>of course</em>&#8211; we want to keep the topology and tiger Sequences.</p>


<pre class="wp-block-code"><span><code class="hljs language-php">	<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">up</span><span class="hljs-params">(Schema $schema)</span>: <span class="hljs-title">void</span>
	</span>{
		<span class="hljs-comment">// this up() migration is auto-generated, please modify it to your needs</span>
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE topology.topology_id_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.county_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.state_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.place_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.cousub_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.edges_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.addrfeat_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.faces_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.featnames_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.addr_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.zcta5_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.tract_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.tabblock_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.bg_gid_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.pagc_gaz_id_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.pagc_lex_id_seq CASCADE'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'DROP SEQUENCE tiger.pagc_rules_id_seq CASCADE'</span>);
	}

	<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">down</span><span class="hljs-params">(Schema $schema)</span>: <span class="hljs-title">void</span>
	</span>{
		<span class="hljs-comment">// this down() migration is auto-generated, please modify it to your needs</span>
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SCHEMA topology'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SCHEMA tiger'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SCHEMA tiger_data'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE topology.topology_id_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.county_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.state_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.place_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.cousub_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.edges_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.addrfeat_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.faces_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.featnames_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.addr_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.zcta5_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.tract_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.tabblock_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.bg_gid_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.pagc_gaz_id_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.pagc_lex_id_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
		<span class="hljs-keyword">$this</span>-&gt;addSql(<span class="hljs-string">'CREATE SEQUENCE tiger.pagc_rules_id_seq INCREMENT BY 1 MINVALUE 1 START 1'</span>);
	}</code></span></pre>


<p>So we have to teach Doctrine to respect these Postgis features.<br />Therefor extend the PostgreSqlPlatform class, add the Postgis features as exclude condition to the query:</p>



<p>src/DBAL/PostgisPostgreSqlPlatform.php:</p>


<pre class="wp-block-code"><span><code class="hljs language-php"><span class="hljs-meta">&lt;?php</span>

<span class="hljs-keyword">namespace</span> <span class="hljs-title">App</span>\<span class="hljs-title">DBAL</span>;

<span class="hljs-keyword">use</span> <span class="hljs-title">Doctrine</span>\<span class="hljs-title">DBAL</span>\<span class="hljs-title">Platforms</span>\<span class="hljs-title">PostgreSQLPlatform</span> <span class="hljs-title">as</span> <span class="hljs-title">PostgreSqlPlatformBase</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PostgisPostgreSqlPlatform</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PostgreSqlPlatformBase</span>
</span>{
	<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getListNamespacesSQL</span><span class="hljs-params">()</span>
	</span>{
		<span class="hljs-comment"># exclude postgis schemas</span>
		<span class="hljs-keyword">return</span> <span class="hljs-string">"SELECT schema_name AS nspname
				FROM   information_schema.schemata
				WHERE  schema_name NOT LIKE 'pg\_%'
				AND schema_name NOT LIKE 'topology'
				AND schema_name NOT LIKE 'tiger%'
				AND    schema_name != 'information_schema'"</span>;
	}

	<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getListSequencesSQL</span><span class="hljs-params">($database)</span>
	</span>{
		<span class="hljs-keyword">return</span> <span class="hljs-string">'SELECT sequence_name AS relname,
					   sequence_schema AS schemaname,
					   minimum_value AS min_value, 
					   increment AS increment_by
				FROM   information_schema.sequences
				WHERE  sequence_catalog = '</span> . <span class="hljs-keyword">$this</span>-&gt;quoteStringLiteral($database) . <span class="hljs-string">"
				AND    sequence_schema NOT LIKE 'pg\_%' 
				AND sequence_schema NOT LIKE 'topology%'
				AND sequence_schema NOT LIKE 'tiger%'
				AND    sequence_schema != 'information_schema'"</span>;
	}
}</code></span></pre>


<p>As Gist: <a href="https://gist.github.com/ivoba/74a143d8074110ef47f93a581bb0c3f6" target="_blank" rel="noreferrer noopener">https://gist.github.com/ivoba/74a143d8074110ef47f93a581bb0c3f6</a></p>



<p>Now tell doctrine to use this PostgreSqlPlatform class instead.<br />In symfony you can add this to config/packages/doctrine.yaml:</p>


<pre class="wp-block-code"><span><code class="hljs language-yaml"><span class="hljs-attr">doctrine:</span>
	<span class="hljs-attr">dbal:</span>
		<span class="hljs-attr">url:</span> <span class="hljs-string">'%env(resolve:DATABASE_URL)%'</span>
		<span class="hljs-attr">types:</span>
			<span class="hljs-attr">geometry:</span> <span class="hljs-string">LongitudeOne\Spatial\DBAL\Types\GeometryType</span>
			<span class="hljs-attr">point:</span> <span class="hljs-string">LongitudeOne\Spatial\DBAL\Types\Geometry\PointType</span>
			<span class="hljs-attr">polygon:</span> <span class="hljs-string">LongitudeOne\Spatial\DBAL\Types\Geometry\PolygonType</span>
			<span class="hljs-attr">linestring:</span> <span class="hljs-string">LongitudeOne\Spatial\DBAL\Types\Geometry\LineStringType</span>
		<span class="hljs-attr">platform_service:</span> <span class="hljs-string">App\DBAL\PostgisPostgreSqlPlatform</span></code></span></pre>


<p>This is inspired by this Gist, which does the same for other Postgres extensions: <a href="https://gist.github.com/dextervip/a2f384050748d6ee3ed7d573425e9d58" target="_blank" rel="noreferrer noopener">https://gist.github.com/dextervip/a2f384050748d6ee3ed7d573425e9d58</a></p>



<p>Now you can run your migrations diff <code>bin/console do:mi:di</code> and the Postgis sequences will be respected by the Doctrine.</p>The post <a href="https://nerdpress.org/2022/10/31/doctrine-migrations-and-postgis/">Doctrine migrations and Postgis</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Migrating user table from Mysql to Postgres with Symfony and Doctrine</title>
		<link>https://nerdpress.org/2021/11/10/migrating-user-table-from-mysql-to-postgres-with-symfony-and-doctrine/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Wed, 10 Nov 2021 08:46:07 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[Postgres]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3087</guid>

					<description><![CDATA[<p>When using bin/console make:entity on Mysql and then later you switch your application to Postgres and you have a table called user, which you most likely have when using security component of Symfony.Then you will receive an error because user is a reserved word in Postgres! An exception occurred while executing 'INSERT INTO user (id, &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2021/11/10/migrating-user-table-from-mysql-to-postgres-with-symfony-and-doctrine/" class="more-link">Continue reading<span class="screen-reader-text"> "Migrating user table from Mysql to Postgres with Symfony and Doctrine"</span></a></p>
The post <a href="https://nerdpress.org/2021/11/10/migrating-user-table-from-mysql-to-postgres-with-symfony-and-doctrine/">Migrating user table from Mysql to Postgres with Symfony and Doctrine</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>When using <code>bin/console make:entity</code> on Mysql and then later you switch your application to Postgres and you have a table called <code>user</code>, which you most likely have when using security component of Symfony.<br />Then you will receive an error because <code>user</code> is a reserved word in Postgres!</p>



<p><code>An exception occurred while executing 'INSERT INTO user (id, email, roles, password, is_verified) VALUES (?, ?, ?, ?, ?)' with params [3, "dev@dev.de", "[]", "your-encrypted-password", 0]:<br />SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "user"<br />LINE 1: INSERT INTO user (id, email, roles, password, is_verified) V...</code></p>



<span id="more-3087"></span>



<p>To fix this you have to escape the table name on your entity, fe. User.php:<br /><br />@<code>ORM\Table(name="`user`")</code></p>



<p>(<strong>note the backticks inside the quotes!)</strong></p>



<p>If you generate the entity with <a href="https://symfony.com/bundles/SymfonyMakerBundle/current/index.html" target="_blank" rel="noreferrer noopener">maker bundle</a>: <code>bin/console make:entity</code> directly on Postgres the backticks are added automatically.<br />But not when you switch the DB type. Then you have to add them manually. :)</p>



<p><br /></p>The post <a href="https://nerdpress.org/2021/11/10/migrating-user-table-from-mysql-to-postgres-with-symfony-and-doctrine/">Migrating user table from Mysql to Postgres with Symfony and Doctrine</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
