<?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>postgresql &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 27 Nov 2021 17:44:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>Create PostgreSQL database for integration testing with Docker</title>
		<link>https://codepills.com/create-postgresql-database-for-integration-testing-with-docker/</link>
					<comments>https://codepills.com/create-postgresql-database-for-integration-testing-with-docker/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Mon, 01 Nov 2021 17:33:29 +0000</pubDate>
				<category><![CDATA[Solutions]]></category>
		<category><![CDATA[CI/CD]]></category>
		<category><![CDATA[Docker]]></category>
		<category><![CDATA[Dockerfile]]></category>
		<category><![CDATA[integration tests]]></category>
		<category><![CDATA[IntelliJ]]></category>
		<category><![CDATA[postgresql]]></category>
		<guid isPermaLink="false">https://codepills.com/?p=1264</guid>

					<description><![CDATA[In this article, we will explore the idea of creating a PostgreSQL database Dockerfile. We can, for example, use it for creating integration tests in a separate CI/CD workflow. <a href="https://codepills.com/create-postgresql-database-for-integration-testing-with-docker/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>In this article, we will explore the idea of creating a PostgreSQL database Dockerfile. We can, for example, use it for creating integration tests in a separate CI/CD workflow.</p>
<p><span id="more-1264"></span></p>
<h2>Introduction</h2>
<p>Let&#8217;s have an idea to create a separate database for integration testing. Creating a separate database will require creating a Docker file with a PostgreSQL database. We can consequently make and run dedicated integration CI/CD workflow for the integration tests. This CI/CD workflow will take our Docker PostgreSQL database and run the database with which integration tests will be working.</p>
<h2>Creating Dockerfile</h2>
<p>We will need to write three files for creating a database testing environment. Technically, we need to write only one file &#8211; <code>Dockerfile</code>. But two other files, database init scripts, will be used to prepare our containerized database for integration tests.</p>
<p>First we need to write PostgreSQL <code>Dockerfile</code>:</p>
<pre><code class="language-yaml">FROM postgres:latest
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD admin
ENV POSTGRES_DB postgres
COPY ./sql/db_user_creation.sql /docker-entrypoint-initdb.d/
COPY ./sql/init_tables.sql /docker-entrypoint-initdb.d/</code></pre>
<p>Let&#8217;s take latest image of PostgreSQL database from posgres Docker vendor. We set up PostgreSQL&#8217;s image environment properties <code>POSTGRES_USER</code>, <code>POSTGRES_PASSWORD</code> and <code>POSTGRES_DB</code> by default values and copy our database init scripts to Docker image entry folder.</p>
<p>The official postgres docker image will run <code>.sql</code> scripts found in the <i>/docker-entrypoint-initdb.d/</i> folder. I highly encourage you to check <a href="https://hub.docker.com/_/postgres" title="Docker Hub Postgres official page" target="_blank" rel="nofollow noopener">official documentation</a> in case in future postgres releases might change this.</p>
<h3>Best practice</h3>
<p>I will go here a little bit sideways. There is a good practice to add on custom-defined <code>Dockerfile</code> last Docker commands from a source image.</p>
<pre><code class="language-yaml">ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]</code></pre>
<p>The last three lines of our <code>Dockerfile</code> are copied from the Postgres image and considered a best practice. However, you can omit them. But in case you want to extend this <code>Dockerfile</code> further, please realize that first, you are running all commands in the Postgres source image. And only then you are running your custom Docker commands. Placing the last command from PostgreSQL&#8217;s image will secure consistent API.</p>
<p>Therefore, the Whole <code>Dockerfile</code> should look like this:</p>
<pre><code class="language-yaml">FROM postgres:latest
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD admin
ENV POSTGRES_DB postgres
COPY ./sql/db_user_creation.sql /docker-entrypoint-initdb.d/
COPY ./sql/init_tables.sql /docker-entrypoint-initdb.d/
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]</code></pre>
<p>The environment variables will instruct the container to create a <i>postgres</i> schema with <i>postgres</i> user (having <i>admin</i> password) on its first run. Any <code>.sql</code> files found in the <i>/docker-entrypoint-initdb.d/</i> of the PostgreSQL container will be executed. If you want to execute <code>.sh</code> scripts, you can also place them in the <i>/docker-entrypoint-initdb.d/</i> folder.</p>
<h2>Custom init scripts</h2>
<p>As you have noticed in <code>Dockerfile</code> description, we have created two other files which we copied to PostgreSQL image. First file is <code>db_user_creation.sql</code>:</p>
<pre><code class="language-sql">CREATE USER mycustomsuperadmin WITH PASSWORD 'changethispassword';
GRANT ALL PRIVILEGES ON DATABASE postgres TO mycustomsuperadmin;</code></pre>
<p>In this file, we could, for example, add a command for creating another database. However, only for demonstration purpose, we will create <i>mycustomsuperadmin</i> user with all the privileges for <i>postgres</i> database. Creating a new user is not mandatory.</p>
<p>The second file we need to create is the initialization of our database with tables and content. This file is also just for demonstration purposes, and you can come with your ideas and needs for what you need to put into the database. Here is example of some database tables we will pad into <i>postgres</i> database.</p>
<pre><code class="language-sql">CREATE TABLE "public".items (
    "id"           integer NOT NULL GENERATED ALWAYS AS IDENTITY (start 1),
    name           varchar(50) NOT NULL,
    price          double precision NOT NULL,
    release        timestamptz(3) NOT NULL,
    CONSTRAINT     PK_Customer PRIMARY KEY ("id", "name", "price", "release")
);

CREATE TABLE "public".users (
    "id"           integer NOT NULL GENERATED ALWAYS AS IDENTITY (start 1),
    name           varchar(50) NOT NULL,
    salary         double precision
    timestamp      timestamptz(3) NOT NULL,
    CONSTRAINT     PK_Customer PRIMARY KEY ("id", "name", "salary", "timestamp")
);</code></pre>
<p>Again, the following code is just for demonstration purposes, and naturally, you can place your own init scripts here.</p>
<p><b>Note</b> : Default syntax for creating database in PostgreSQL is &#8220;database_name&#8221;.&#8221;scheme_name&#8221;.&#8221;table_name&#8221;.</p>
<h2>Build Dockerfile</h2>
<p>Now, when we have all the files, we need to run the Docker build command. So, go to the folder where you placed PostgreSQL <code>Dockerfile</code> and run Docker build command like this:</p>
<h3>IntelliJ configuration</h3>
<p>You can easily build and run your Docker image from IntelliJ. Check the image of configuration for setting up the file:</p>
<p><img fetchpriority="high" decoding="async" src="https://codepills.com/wp-content/uploads/2021/11/intellij_postgresql_docker_build_runner_settings.png" alt="IntelliJ PostgreSQL Docker build runner settings" width="635" height="522" class="alignnone size-full wp-image-1265" srcset="https://codepills.com/wp-content/uploads/2021/11/intellij_postgresql_docker_build_runner_settings.png 635w, https://codepills.com/wp-content/uploads/2021/11/intellij_postgresql_docker_build_runner_settings-300x247.png 300w" sizes="(max-width: 635px) 100vw, 635px" /></p>
<pre><code class="language-yaml">Docker build .</code></pre>
<p>Hitting the following setup in IntelliJ will build your <code>Dockerfile</code> and place it in your local Docker repository.</p>
<h2>Alternative with docker-compose</h2>
<p>There is alternative way how to build and run PostgreSQL locally. It is without creating Docker image and instead just run <code>docker-compose</code>. Here is a code for <code>docker-compose.yaml</code> file:</p>
<pre><code class="language-yml">version: '3.7'
services:
    postgres:
        image: postgres:latest
        restart: always
        environment:
            POSTGRES_USER: postgres
            POSTGRES_PASSWORD: admin
            POSTGRES_DB: postgres
        logging:
            options:
                max-size: 10m
                max-file: "3"
        ports:
            - '5432:5432'
        volumes:
            - ./postgres-data:/var/lib/postgresql/data
            # copy the sql script to create tables
            - .sql/init_tables.sql:/docker-entrypoint-initdb.d/init_tables.sql
            # copy the sql script to create user
            - .sql/db_user_creation.sql:/docker-entrypoint-initdb.d/db_user_creation.sql</code></pre>
<p>Running <code>docker-compose up -d</code> will run docker-compose script in your repository. To turn off the service, execute command <code>docker-compose down</code>.</p>
<p><b>Node</b>: So if you placed your Dockerfile in <i>project-root/src/test/resources</i> and you are in your command prompt on this sys-path, running docker-compose might first run docker-compose in your <i>project-root/</i> folder.</p>
<h2>Conclusion</h2>
<p>This article has shown us how to create a PostgreSQL database Dockerfile. However, we can develop this idea further and, for example, make it for integration tests in a separate CI/CD workflow.</p>
<p>Did you find creating <code>Dockerfile</code> easy? Do you have your trick or know another way <u>how to create Dockerfile</u>? Let us know in the comments below the article. We would like to hear your ideas and stories.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/create-postgresql-database-for-integration-testing-with-docker/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Convert varchar to text in PostgreSQL</title>
		<link>https://codepills.com/convert-varchar-to-text-in-postgresql/</link>
					<comments>https://codepills.com/convert-varchar-to-text-in-postgresql/#respond</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Fri, 11 May 2018 12:00:37 +0000</pubDate>
				<category><![CDATA[Language basics]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[varchar]]></category>
		<guid isPermaLink="false">http://codepills.com/?p=961</guid>

					<description><![CDATA[This article will show you a simple way how to convert <code>VARCHAR</code> data type to <code>TEXT</code> data type. <a href="https://codepills.com/convert-varchar-to-text-in-postgresql/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>If you work with PostgreSQL, you will get into the situation when you want to change the data type for the text fields in your tables. This article is about simple way how to convert <code>VARCHAR</code> data type to <code>TEXT</code> data type.</p>
<p><span id="more-961"></span></p>
<p>The simple command to alter a column in the database is with <code>TEXT</code> data type looks like this:</p>
<pre><code class="language-sql">ALTER TABLE table_name ALTER COLUMN column_name TYPE text;</code></pre>
<p>The table with table_name alters column with colum_name for <code>TEXT</code> type. That is all you need to do to modify the column data type. The question is, what will happen with data if the table is not empty? Frankly, nothing. <code>TEXT</code> is the data type of unlimited length; therefore, it will adjust to the maximum length of stored data.</p>
<p>Let&#8217;s imagine we have a table <code>companies</code> in our project where we store all the information about companies. From the start, we have specified very precisely which field will have what length. We used <code>VARCHAR</code> with a fixed length. For example name of the company could be empty or have 128 characters, email 32 characters, etc. Soon we realize this length limitation is useless. Companies can have very long names; the email address can be longer than 32 characters, and so on and on.</p>
<p>This is an example how you can make multiple alterations to our companies table:</p>
<pre><code class="language-sql">ALTER TABLE project.companies
  ALTER COLUMN name TYPE TEXT;
ALTER TABLE project.companies
  ALTER COLUMN street TYPE TEXT;
ALTER TABLE project.companies
  ALTER COLUMN city TYPE TEXT;
ALTER TABLE project.companies
  ALTER COLUMN post_code TYPE TEXT;
ALTER TABLE project.companies
  ALTER COLUMN email_address TYPE TEXT;
ALTER TABLE project.companies
  ALTER COLUMN phone_number TYPE TEXT;
ALTER TABLE project.companies
  ALTER COLUMN web_address TYPE TEXT;</code></pre>
<p>The example above shows the correct way of multiple alterations. Although it contains a lot of repetition. There is another way how to write less code in PostgreSQL. We can batch the commands together and write much concise code. The whole block of code will look like this:</p>
<pre><code class="language-sql">ALTER TABLE project.companies
ALTER COLUMN name TYPE TEXT,
ALTER COLUMN street TYPE TEXT,
ALTER COLUMN city TYPE TEXT,
ALTER COLUMN post_code TYPE TEXT,
ALTER COLUMN email_address TYPE TEXT,
ALTER COLUMN phone_number TYPE TEXT,
ALTER COLUMN web_address TYPE TEXT;</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/convert-varchar-to-text-in-postgresql/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
