<?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>Linux Guides, Tips and Tutorials | LinuxScrew</title>
	<atom:link href="https://www.linuxscrew.com/feed" rel="self" type="application/rss+xml" />
	<link>https://www.linuxscrew.com</link>
	<description>LinuxScrew is a Linux resource that contains many useful guides on Linux, programming, networking, and more.</description>
	<lastBuildDate>Thu, 10 Aug 2023 00:00:18 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.2.6</generator>

<image>
	<url>https://www.linuxscrew.com/wp-content/uploads/2020/10/favicon-7-48x48.png</url>
	<title>Linux Guides, Tips and Tutorials | LinuxScrew</title>
	<link>https://www.linuxscrew.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Installing/Upgrading VirtualBox Guest Additions on Linux Desktop/Server</title>
		<link>https://www.linuxscrew.com/install-upgrade-virtualbox-guest-additions-linux</link>
					<comments>https://www.linuxscrew.com/install-upgrade-virtualbox-guest-additions-linux#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Thu, 10 Aug 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Virtualization]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16353</guid>

					<description><![CDATA[<p>This article will describe how to install or update VirtualBox Guest Additions in Linux, showing the commands you need to run. VirtualBox is a free, open source virtualization platform that allows you to create virtual machines that run on your Linux system. These virtual machines act just like their own self-contained computers, and can run other Linux distributions, or even other operating systems entirely, such as Windows, or MS-DOS. Virtual Machines &#8211; Hosts vs Guests In virtualization terminology, the&#160;Host&#160;is the computer that runs a virtual ... <a title="Installing/Upgrading VirtualBox Guest Additions on Linux Desktop/Server" class="read-more" href="https://www.linuxscrew.com/install-upgrade-virtualbox-guest-additions-linux" aria-label="More on Installing/Upgrading VirtualBox Guest Additions on Linux Desktop/Server">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/install-upgrade-virtualbox-guest-additions-linux">Installing/Upgrading VirtualBox Guest Additions on Linux Desktop/Server</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This article will describe how to install or update VirtualBox Guest Additions in Linux, showing the commands you need to run.</p>



<p>VirtualBox is a free, open source <a href="https://www.linuxscrew.com/category/linux/virtualization">virtualization platform</a> that allows you to create virtual machines that run on your Linux system. These virtual machines act just like their own self-contained computers, and can run other Linux distributions, or even other operating systems entirely, such as Windows, or MS-DOS.</p>



<h2 class="wp-block-heading" id="virtual-machines---hosts-vs-guests">Virtual Machines &#8211; Hosts vs Guests</h2>



<p>In virtualization terminology, the&nbsp;<em>Host</em>&nbsp;is the computer that runs a virtual machine, and a&nbsp;<em>Guest</em>&nbsp;is the virtualized computer running in the virtual machine.</p>



<p>The&nbsp;<a href="https://www.virtualbox.org/" target="_blank" rel="noopener">VirtualBox</a>&nbsp;software is installed on the host, and is used to manage and run guest virtual machines.</p>



<h2 class="wp-block-heading" id="virtualbox-guest-additions">VirtualBox Guest Additions</h2>



<p>The&nbsp;<a href="https://www.virtualbox.org/manual/ch04.html" target="_blank" rel="noopener">VirtualBox Guest Additions</a>&nbsp;is a software package that is installed on the&nbsp;<em>Guest</em>&nbsp;&#8211; the virtual machine. It provides drivers to make the guest operating system run properly &#8211; allowing it to use the virtual network interface and fully utilize the system resources allocated to it, as well as extra software to make working with the virtual machine easier, such as sharing folders between the guest and host, or allowing your mouse-pointer to move seamlessly between the two.</p>



<h2 class="wp-block-heading" id="installing-virtualbox-guest-additions">Installing VirtualBox Guest Additions</h2>



<p>Here are the steps you need to follow to install the guest additions in Linux &#8211; we&#8217;ll do it from the command line, so it will work on both desktop and server distributions.</p>



<p><strong>All of the following commands are to be run on the guest!</strong></p>



<h3 class="wp-block-heading" id="installing-requirements---red-hat-linux">Installing Requirements &#8211; Red Hat Linux</h3>



<p>First, you&#8217;ll need to run the following command to install the software required to build and install the guest additions package:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo dnf install tar bzip2 kernel-devel kernel-headers perl gcc make elfutils-libelf-devel
</pre>



<p>If you do not do this, you&#8217;ll get an error about not being able to build kernel modules, and the guest additions will not be fully installed.</p>



<h2 class="wp-block-heading" id="installing-requirements---ubuntudebian-linux">Installing Requirements &#8211; Ubuntu/Debian Linux</h2>



<p>If you&#8217;re on Ubuntu/Debian, run the following to install dependencies:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo apt install build-essential dkms linux-headers-generic
</pre>



<h2 class="wp-block-heading" id="attach-the-virtualbox-guest-additions-cd-image">Attach the VirtualBox Guest Additions CD Image</h2>



<p>VirtualBox Guest Additions are supplied in a CD image with VirtualBox, and are available to every virtual machine that you create. To attach the CD image so that the guest can read it, open your virtual machine and click&nbsp;<em>Devices</em>&nbsp;in the menu bar, then click&nbsp;<em>Insert Guest Additions CD Image&#8230;</em>.</p>



<h2 class="wp-block-heading" id="mount-the-cd-image">Mount the CD Image</h2>



<p>If the image does not mount automatically (likely the case if you are using a server-focussed Linux distribution), mount it by running the following commands:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mkdir /media/cdrom
sudo mount /dev/cdrom /media/cdrom
</pre>



<p>Then, navigate to the mounted CD-ROM by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">cd /media/cdrom
</pre>



<h2 class="wp-block-heading" id="install-virtualbox-guest-additions-for-linux">Install VirtualBox Guest Additions for Linux</h2>



<p>Finally, run the following command to perform the installation:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo ./VBoxLinuxAdditions.run
</pre>



<p>You will see something like the following to indicate that the installation has started:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Verifying archive integrity... All good.
Uncompressing VirtualBox 6.1.40 Guest Additions for Linux.
VirtualBox Guest Additions installer
</pre>



<p>Additional output will be presented, and the process may take several minutes.</p>



<p>When the installation has successfully completed, reboot:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo reboot
</pre>



<p>You can confirm the modules have been installed successfully by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lsmod -l | grep -io vboxguest</pre>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/install-upgrade-virtualbox-guest-additions-linux">Installing/Upgrading VirtualBox Guest Additions on Linux Desktop/Server</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/install-upgrade-virtualbox-guest-additions-linux/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL Configuration: Port/Host, Memory, Temp File Limits</title>
		<link>https://www.linuxscrew.com/postgresql-configuration-port-host-memory-temp-file-limits</link>
					<comments>https://www.linuxscrew.com/postgresql-configuration-port-host-memory-temp-file-limits#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Wed, 09 Aug 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16360</guid>

					<description><![CDATA[<p>This article will show you how to find your PostgreSQL configuration file, and change the port and host, maximum connections, temporary file and memory limits for PostgreSQL on Linux. Once you have your PostgreSQL database server up and running, you&#8217;ll probably want to tweak the configuration. The most common things you will want to change are the port and host bindings, and memory and file size limits if you want to tweak the performance of your server. Finding the PostgreSQL Configuration File The PostgreSQL configuration ... <a title="PostgreSQL Configuration: Port/Host, Memory, Temp File Limits" class="read-more" href="https://www.linuxscrew.com/postgresql-configuration-port-host-memory-temp-file-limits" aria-label="More on PostgreSQL Configuration: Port/Host, Memory, Temp File Limits">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-configuration-port-host-memory-temp-file-limits">PostgreSQL Configuration: Port/Host, Memory, Temp File Limits</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This article will show you how to find your PostgreSQL configuration file, and change the port and host, maximum connections, temporary file and memory limits for PostgreSQL on Linux.</p>



<p>Once you have your PostgreSQL database server up and running, you&#8217;ll probably want to tweak the configuration. The most common things you will want to change are the port and host bindings, and memory and file size limits if you want to tweak the performance of your server.</p>



<h2 class="wp-block-heading" id="finding-the-postgresql-configuration-file">Finding the PostgreSQL Configuration File</h2>



<p>The PostgreSQL configuration file is usually named&nbsp;<em>postgresql.conf</em>&nbsp;and by default is located in the data directory of your PostgreSQL installation. The location of the PostgreSQL data directory will vary depending on the Linux distribution you are using, and how you installed PostgreSQL. Generally, however, you should just be able to issue the following command to get the path to the PostgreSQL configuration file for the&nbsp;<a href="https://www.linuxscrew.com/postgresql-switch-active-cluser-default-port">active PostgreSQL server</a>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pg_config --sysconfdir
</pre>



<h3 class="wp-block-heading" id="ubuntudebian">Ubuntu/Debian</h3>



<p>The default location for the PostgreSQL configuration file in Ubuntu and Debian Linux is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/etc/postgresql/&lt;POSTGRESQL_VERSION&gt;/main/postgresql.conf
</pre>



<p>Note that the path will differ for each version of PostgreSQL installed.</p>



<h3 class="wp-block-heading" id="red-hatfedora">Red Hat/Fedora</h3>



<p>The default location for the PostgreSQL configuration file on Red Hat and Fedora based systems is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/var/lib/pgsql/&lt;POSTGRESQL_VERSION&gt;/data/postgresql.conf.
</pre>



<p>Note that the path will differ for each version of PostgreSQL installed.</p>



<h3 class="wp-block-heading" id="arch-linux">Arch Linux</h3>



<p>The default location for the PostgreSQL configuration file in Arch Linux is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/var/lib/postgres/data/postgresql.conf
</pre>



<p>Note that as Arch only installs the latest version of PostgreSQL from its package manager, the version number is not included in the path.</p>



<h3 class="wp-block-heading" id="other-linux-distributions">Other Linux Distributions</h3>



<p>If you cannot find your PostgreSQL configuration file, or you manually installed the server and placed it at a different path, the following command will find all files named&nbsp;<em>postgresql.conf</em>&nbsp;and list their path:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">find / -name "postgresql.conf" 2&gt;/dev/null
</pre>



<h2 class="wp-block-heading" id="editing-the-postgresql-configuration-file">Editing the PostgreSQL Configuration File</h2>



<p>You&#8217;ll need to use a text editor to edit the below values in&nbsp;<em>postgresql.conf</em>.&nbsp;<a href="https://linux.die.net/man/1/nano" target="_blank" rel="noopener">nano</a>&nbsp;is the easiest text editor to use for the Linux command line, and can be used to open or create a text file with the following command:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">nano /path/to/postgresql.conf
</pre>



<p>To edit system configuration files, you also need root privileges using the&nbsp;<a href="https://www.linuxscrew.com/why-use-sudo-instead-of-su">sudo command</a>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo nano /path/to/postgresql.conf
</pre>



<p>To find a specific configuration line, you can search in nano by pressing&nbsp;<strong>CTRL + W</strong></p>



<p>To save and exit in&nbsp;<em>nano</em>, press the&nbsp;<strong>CTRL + X</strong>&nbsp;keys.</p>



<p>After you have edited PostgreSQl configuration file, you will need to&nbsp;<a href="https://www.linuxscrew.com/postgresql-start-stop-restart">restart the PostgreSQL database service</a>&nbsp;for the changes to take effect.</p>



<p>If the configuration line that you are editing begins with a hash (<strong>#</strong>), it is commented &#8211; and inactive, so you must remove the hash character from the line before changing the value. For example:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># port = 5432
</pre>



<p>&#8230;is commented, whereas</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">port = 5432
</pre>



<p>&#8230; is not.</p>



<h2 class="wp-block-heading" id="changing-the-postgresql-database-server-port">Changing the PostgreSQL Database Server Port</h2>



<p>There are several reasons why you may want to change the port PostgreSQL is running on &#8211; you may want to run multiple PostgreSQL servers from a single server (each requiring their own port), or you may want to use a non-standard port to pass it through your firewall.</p>



<p>To change the PostgreSQL server port, find the following line in&nbsp;<em>postgresql</em>.conf</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">port = 5432
</pre>



<p><strong>5432</strong>&nbsp;is the default port for PostgreSQL. Change this to another valid port number and save.</p>



<h2 class="wp-block-heading" id="changing-the-postgresql-server-hostaddress">Changing the PostgreSQL Server Host/Address</h2>



<p>In short, when a service &#8216;listens&#8217; on an address, it responds to network requests that match that address only.</p>



<p>By default, PostgreSQL listens on the hostname&nbsp;<em>localhost</em>, allowing only connections from the local machine:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">listen_addresses = 'localhost'
</pre>



<p>To set PostgreSQl to respond on all available network addresses, a wildcard value can be used:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">listen_addresses = *
</pre>



<p>PostgreSQL can also be set to listen only on a specific interface or IP address. You may want to do this if you want to restrict access to the server only to known IP addresses, or if your server is connected to multiple networks (eg public and private) and want to limit access to only a subset of them:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">listen_addresses = 192.168.1.10
</pre>



<p>To function, the&nbsp;<em>listen_addresses</em>&nbsp;value must be for an address associated with the server.</p>



<p>Multiple listen_addresses can be defined, separated by commas, if your server is connected to multiple networks:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">listen_addresses = 192.168.1.10,192.168.2.10
</pre>



<p>Host Names can also be used, and mixed with IP addresses:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">listen_addresses = 192.168.1.10,192.168.2.10,localhost,myServerName
</pre>



<h2 class="wp-block-heading" id="setting-the-maximum-number-of-server-connections">Setting the Maximum Number of Server Connections</h2>



<p>PostgreSQL can handle a large number of simultaneous database connections, but you may want to limit this if your server regularly becomes overloaded during periods of high demand. Update the maximum number of connections the server will accept using the below configuration line:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">max_connections = 100	
</pre>



<p>Note that once the connection limit is hit, your users will receive errors and not be able to use the database until the number of connections drops, so you may want to implement&nbsp;<a href="https://en.wikipedia.org/wiki/Connection_pool" target="_blank" rel="noopener">connection pooling</a>&nbsp;in your code, or reduce the number of queries, to mitigate this.</p>



<h2 class="wp-block-heading" id="setting-the-postgresql-memory-limit">Setting the PostgreSQL Memory Limit</h2>



<p>If you are running your PostgreSQL alongside other services, like a web server, file server, or just running it on your local computer for development and don&#8217;t want it using too many resources, you can limit the memory usage using the following configuration line:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">shared_buffers = 128MB	
</pre>



<p>Alternatively, you may want to&nbsp;<em>raise</em>&nbsp;the memory limit if your database gets very busy.</p>



<h2 class="wp-block-heading" id="setting-postgresql-temporary-file-limits">Setting PostgreSQL Temporary File Limits</h2>



<p>While performing queries, especially complex ones on large amounts of data, PostgreSQL may need to store temporary files on your servers storage. If your server has a limited amount of space, you can set a limit to the size of these temporary files on the following configuration line:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">temp_file_limit = -1	
</pre>



<p>Note that a value of&nbsp;<em>-1</em>&nbsp;means that there is&nbsp;<strong>no limit</strong>!</p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-configuration-port-host-memory-temp-file-limits">PostgreSQL Configuration: Port/Host, Memory, Temp File Limits</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-configuration-port-host-memory-temp-file-limits/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Change A User Password in PostgreSQL</title>
		<link>https://www.linuxscrew.com/postgresql-change-user-password</link>
					<comments>https://www.linuxscrew.com/postgresql-change-user-password#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Tue, 08 Aug 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16383</guid>

					<description><![CDATA[<p>This article will explain how to change a user&#8217;s password in PostgreSQL. PostgreSQL is one of the most popular database systems due to its flexibility and support for&#160;granular permissions&#160;and&#160;user roles&#160;that allow you to assign permissions to a group of users. Connect to the PostgreSQL Server as an Administrative User To change a password for a user in PostgreSQL, you must first connect with the&#160;psql&#160;client: Above, the&#160;psql&#160;command is used to connect to the PostgreSQL server on&#160;localhost, on port&#160;5432, as the default&#160;postgres&#160;user. The default&#160;postgres&#160;user has administrative rights, ... <a title="How to Change A User Password in PostgreSQL" class="read-more" href="https://www.linuxscrew.com/postgresql-change-user-password" aria-label="More on How to Change A User Password in PostgreSQL">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-change-user-password">How to Change A User Password in PostgreSQL</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This article will explain how to change a user&#8217;s password in PostgreSQL. PostgreSQL is one of the most popular database systems due to its flexibility and support for&nbsp;<a href="https://www.linuxscrew.com/create-databases-users-permissions">granular permissions</a>&nbsp;and&nbsp;<a href="https://www.linuxscrew.com/postgresql-users-roles">user roles</a>&nbsp;that allow you to assign permissions to a group of users.</p>



<h2 class="wp-block-heading" id="connect-to-the-postgresql-server-as-an-administrative-user">Connect to the PostgreSQL Server as an Administrative User</h2>



<p>To change a password for a user in PostgreSQL, you must first connect with the&nbsp;<a href="https://www.linuxscrew.com/postgresql-how-to-connect"><em>psql</em>&nbsp;client</a>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">psql -h localhost -p 5432 -U postgres
</pre>



<p>Above, the&nbsp;<em>psql</em>&nbsp;command is used to connect to the PostgreSQL server on&nbsp;<em>localhost</em>, on port&nbsp;<em>5432</em>, as the default&nbsp;<em>postgres</em>&nbsp;user.</p>



<p>The default&nbsp;<em>postgres</em>&nbsp;user has administrative rights, has permission to alter the passwords of other users. If you are logging in as a different user, they will need these permissions granted to them.</p>



<h2 class="wp-block-heading" id="updating-a-users-password-in-postgresql">Updating a Users Password in PostgreSQL</h2>



<p>Once connected to the PostgreSQL server, you can update a users password using the&nbsp;<a href="https://www.postgresql.org/docs/current/sql-alteruser.html" target="_blank" rel="noopener">ALTER USER</a>&nbsp;statement:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ALTER USER user_name WITH PASSWORD 'new_password';
</pre>



<p>Note that you&#8217;ll need to replace&nbsp;<em>user_name</em>&nbsp;and&nbsp;<em>new_password</em>&nbsp;with the username of the user you want to update the password for, and their new password, respectively.</p>



<h2 class="wp-block-heading" id="checking-that-the-password-has-updated-successfully">Checking that the Password has Updated Successfully</h2>



<p>To check that the password change was successful, you can attempt to log in as the user:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">psql -h localhost -p 5432 -U user_namename
</pre>



<p>After entering this command you will be prompted for a password for the login. If the password change was successful, the login attempt will be successful.</p>



<p>If not, re-check the command you used to change the password, making sure that you have typed the password correctly and that no error occured.</p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-change-user-password">How to Change A User Password in PostgreSQL</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-change-user-password/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Set Up Users and Roles in PostgreSQL</title>
		<link>https://www.linuxscrew.com/postgresql-users-roles</link>
					<comments>https://www.linuxscrew.com/postgresql-users-roles#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Mon, 07 Aug 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16377</guid>

					<description><![CDATA[<p>This article will show you how to set up user roles in PostgreSQL, and provide code examples. User roles are one of the best features of the PostgreSQL database system. This feature lets you group users and assign them permissions, making managing your database permissions and security much, much easier. What are PostgreSQL&#8217;s User Roles? In most database management systems, permission (such as being able to read or write to a database or table within a database) are assigned directly to user accounts in the ... <a title="How to Set Up Users and Roles in PostgreSQL" class="read-more" href="https://www.linuxscrew.com/postgresql-users-roles" aria-label="More on How to Set Up Users and Roles in PostgreSQL">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-users-roles">How to Set Up Users and Roles in PostgreSQL</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This article will show you how to set up user roles in PostgreSQL, and provide code examples. User roles are one of the best features of the PostgreSQL database system. This feature lets you group users and <a href="https://www.linuxscrew.com/postgresql-list-psermissions">assign them permissions</a>, making managing your database permissions and security much, much easier.</p>



<h2 class="wp-block-heading" id="what-are-postgresqls-user-roles">What are PostgreSQL&#8217;s User Roles?</h2>



<p>In most database management systems, permission (such as being able to read or write to a database or table within a database) are assigned directly to user accounts in the database system. While this works, it&#8217;s not really convenient if you are dealing with a lot of users and a lot of databases.</p>



<p>User roles in PostgreSQL lets you assign permissions to&nbsp;<em>roles</em>&nbsp;rather than directly to users, and then assign those roles to users. This abstraction makes it easier to manage your databases, and improves security as it is less likely that you will accidentally grant the wrong permissions to the wrong user.</p>



<p>The best example of this is being able to update the permission for a role, with those permissions then being applied to all users in that role, rather than having to update (potentially tens or hundreds) of user accounts individually.</p>



<p>Users can have many roles, and the permissions from each will be applicable, as well as any permissions granted to users individually. As PostgreSQL&#8217;s permissions are restricted by default, and you are only granting permissions to&nbsp;<em>allow</em>&nbsp;something, there are no conflicts.</p>



<h2 class="wp-block-heading" id="how-to-create-user-roles-in-postgresql">How to Create User Roles in PostgreSQL</h2>



<p>The&nbsp;<a href="https://www.postgresql.org/docs/current/sql-createrole.html" target="_blank" rel="noopener">CREATE_ROLE</a>&nbsp;statement is used to create new user roles in PostgreSQL:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">CREATE ROLE my_role WITH LOGIN;
</pre>



<p>Note the use of&nbsp;<em>WITH LOGIN</em>&nbsp;&#8211; this grants the&nbsp;<em>LOGIN</em>&nbsp;permission to the role from the outset, otherwise, users with that role would not be able to log in unless they had been granted that permission individually.</p>



<h2 class="wp-block-heading" id="granting-additional-permissions-to-a-role">Granting Additional Permissions to a Role</h2>



<p>The&nbsp;<a href="https://www.postgresql.org/docs/current/sql-grant.html" target="_blank" rel="noopener">GRANT</a>&nbsp;statement is use to grant permissions to a role once it has been created:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">GRANT CONNECT, SELECT ON DATABASE my_database TO my_role;
</pre>



<p>Above, the role&nbsp;<em>my_role</em>&nbsp;is granted permission to&nbsp;<em>CONNECT</em>&nbsp;to the database&nbsp;<em>my_database</em>&nbsp;and&nbsp;<em>SELECT</em>&nbsp;data from it.</p>



<p><a href="https://linuxscrew.com/postgresql-create-databases-users-permissions" target="_blank" rel="noopener">A list of available PostgreSQL database permissions can be found here</a>.</p>



<h2 class="wp-block-heading" id="assigning-roles-to-users">Assigning Roles to Users</h2>



<p>The GRANT command is also used to assign roles to users:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">GRANT my_role TO my_user;
</pre>



<h2 class="wp-block-heading" id="removing-roles-from-users">Removing Roles from Users</h2>



<p>The&nbsp;<a href="https://www.postgresql.org/docs/current/sql-revoke.html" target="_blank" rel="noopener">REVOKE</a>&nbsp;statement is used to remove a user from a role:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">REVOKE my_role FROM my_user;</pre>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-users-roles">How to Set Up Users and Roles in PostgreSQL</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-users-roles/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Enabling PostgreSQL Logging and Query Logging</title>
		<link>https://www.linuxscrew.com/postgresql-enable-logging-and-query-logging</link>
					<comments>https://www.linuxscrew.com/postgresql-enable-logging-and-query-logging#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Sun, 06 Aug 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16365</guid>

					<description><![CDATA[<p>This article explains how to enable query logging in PostgreSQL. One of the most useful features of PostgreSQL includes is query logging, which allows you to better understand the performance of your databases and the queries you run on them. PostgreSQL Log files PostgreSQL&#8217;s log files will contain any error output from the server process, as well as status from the process. These logs will be created in the directory: In addition to this, you can&#160;log queries themselves&#160;for diagnostics and reporting. Updating the PostgreSQL Configuration ... <a title="Enabling PostgreSQL Logging and Query Logging" class="read-more" href="https://www.linuxscrew.com/postgresql-enable-logging-and-query-logging" aria-label="More on Enabling PostgreSQL Logging and Query Logging">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-enable-logging-and-query-logging">Enabling PostgreSQL Logging and Query Logging</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This article explains how to enable query logging in PostgreSQL. One of the most useful features of PostgreSQL includes is query logging, which allows you to better understand the performance of your databases and the queries you run on them. </p>



<h2 class="wp-block-heading" id="postgresql-log-files">PostgreSQL Log files</h2>



<p>PostgreSQL&#8217;s log files will contain any error output from the server process, as well as status from the process. These logs will be created in the directory:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/var/logs/
</pre>



<p>In addition to this, you can&nbsp;<a href="https://www.postgresql.org/docs/current/runtime-config-logging.html" target="_blank" rel="noopener">log queries themselves</a>&nbsp;for diagnostics and reporting.</p>



<h2 class="wp-block-heading" id="updating-the-postgresql-configuration-file">Updating the PostgreSQL Configuration File</h2>



<p>Modifying PostgreSQL&#8217;s logging options is done through its configuration file. You can find the file by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo -u postgres psql -c 'SHOW config_file'
</pre>



<p>Note the use of the&nbsp;<a href="https://www.linuxscrew.com/why-use-sudo-instead-of-su"><em>sudo</em>&nbsp;command</a> to run the command as the&nbsp;<em>posgres</em>&nbsp;system user.</p>



<h2 class="wp-block-heading" id="enabling-logging-in-postgresql-configuration">Enabling Logging in PostgreSQL Configuration</h2>



<p>Once you have located the configuration file currently in use by PostgreSQL, edit it by running</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo nano /path/to/postgresql.conf
</pre>



<p>Then, find the following line:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#log_statement = 'none'
</pre>



<p>Uncomment it, and change&nbsp;<em>none</em>&nbsp;to&nbsp;<em>all</em>&nbsp;so it looks like the following:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">log_statement = 'all'
</pre>



<p>The other options for this configuration setting are:</p>



<ul><li><strong>ddl</strong>&nbsp;&#8211; Logs all definition statements, such as CREATE, ALTER, and DROP.</li><li><strong>mod</strong>&nbsp;&#8211; Logs all statements from ddl, as well as data-modifying statements like INSERT, UPDATE, DELETE, TRUNCATE, and COPY FROM. PREPARE, EXECUTE, and EXPLAIN ANALYZE statements are also logged if their content is appropriate.</li><li><strong>all</strong>&nbsp;&#8211; Logs all statements.</li></ul>



<p>To save and quit in&nbsp;<em>nano</em>, press&nbsp;<em>CTRL + X</em>.</p>



<h2 class="wp-block-heading" id="query-log-destination">Query Log Destination</h2>



<p>You can also change the destination of the query log files &#8211; this directory must be writable by the&nbsp;<em>postgresql</em>&nbsp;user.</p>



<p>Change this by editing the&nbsp;<em>log_destination</em>&nbsp;entry in your PostgreSQL configuration file.</p>



<p>By default, query logs will be stored in the directory&nbsp;<em>pg_log</em>&nbsp;in the PostgreSQL data directory, which can be found by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo -u postgres psql -c 'SHOW data_directory'
</pre>



<h2 class="wp-block-heading" id="applying-postgresql-configuration-changes">Applying PostgreSQL Configuration Changes</h2>



<p>Once you&#8217;ve updated your configuration, restart the PostgreSQL service by following our instructions here.</p>



<p>Queries executed will now be logged to the location configured in the previous step. The log files will named&nbsp;<em>postgresql-YYYY-MM-DD_HHMMSS.log</em>&nbsp;and will contain details about each query, including the query string, and execution time.</p>



<p>Query logs can take up a lot of disk space, especially if your database is busy. It&#8217;s best to periodically clear these files, or, rotate them out so that old files are regularly cleared out as new ones are created, reducing disk usage.&nbsp;<a href="https://linux.die.net/man/8/logrotate" target="_blank" rel="noopener">logrotate</a>&nbsp;is included with most Linux distributions and does just this.</p>



<p>To configure&nbsp;<em>logrotate</em>, add a configuration file for PostgreSQL query logging by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo nano /etc/logrotate/.d/postgresql-query
</pre>



<p>&#8230;and adding the following configuration to the file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/path/to/postgresql-*.log {
size 50M
rotate 10
compress
delaycompress
missingok
notifempty
copytruncate
}
</pre>



<p>This configuration will keep only the most recent 10 log files and compress them to save disk space. A limit of 50 megabytes will also be imposed. The&nbsp;<em>logrotate</em>&nbsp;utility will run periodically to rotate the log files according to this configuration.</p>



<p>Once the configuration has been updated, run:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">sudo service logrotate restart
</pre>



<p>&#8230;to apply it by restarting the&nbsp;<em>logrotate</em>&nbsp;service.</p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-enable-logging-and-query-logging">Enabling PostgreSQL Logging and Query Logging</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-enable-logging-and-query-logging/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Comment PostgreSQL Queries/Code, and the PostgreSQL COMMENT Statement</title>
		<link>https://www.linuxscrew.com/postgresql-comments</link>
					<comments>https://www.linuxscrew.com/postgresql-comments#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Mon, 31 Jul 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16389</guid>

					<description><![CDATA[<p>This short article will explain the difference between comments in PostgreSQL code and the COMMENT statement, and show you how to use both. It will show you how to add comments to your PostgreSQL query code, so that you can leave yourself (and others) notes to remind you what the code does, or explain any tricky queries you&#8217;ve constructed. Comments in PostgreSQL Code When writing the code that forms your PostgreSQL queries, you can leave comments in two ways. The first is to use two ... <a title="How to Comment PostgreSQL Queries/Code, and the PostgreSQL COMMENT Statement" class="read-more" href="https://www.linuxscrew.com/postgresql-comments" aria-label="More on How to Comment PostgreSQL Queries/Code, and the PostgreSQL COMMENT Statement">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-comments">How to Comment PostgreSQL Queries/Code, and the PostgreSQL COMMENT Statement</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This short article will explain the difference between comments in PostgreSQL code and the COMMENT statement, and show you how to use both. It will show you how to add comments to your PostgreSQL query code, so that you can leave yourself (and others) notes to remind you what the code does, or explain any tricky queries you&#8217;ve constructed.</p>



<h2 class="wp-block-heading" id="comments-in-postgresql-code">Comments in PostgreSQL Code</h2>



<p>When writing the code that forms your <a href="https://www.linuxscrew.com/?s=postgresql">PostgreSQL queries</a>, you can leave comments in two ways.</p>



<p>The first is to use two dashes preceding the commented line to leave a single-line comment (<strong>&#8212;</strong>):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">-- This is a single line PostgreSQL comment
</pre>



<p>The second is to use the&nbsp;<strong>/*</strong>&nbsp;and&nbsp;<strong>*/</strong>&nbsp;symbols to mark the beginning and end of comments, which can span multiple lines prepended with&nbsp;<strong>*</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/* This is a single line comment */

/*
* This is a 
* multiline
* comment
*/
</pre>



<p>Comments can be placed standalone on their own lines:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">/* Get a list of usernames and emails from the table tbl_users  */
SELECT username, email   
FROM tbl_users;
</pre>



<p>&#8230;Or follow at the end of a line of code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">SELECT username, email  /* Get a list of usernames and emails */
FROM tbl_users;         /* from the table tbl_users  */
</pre>



<p>This method is fine for commenting your inline PostgreSQL queries in your code, or in the scripts that you will run when to populate your database &#8211; but be aware &#8211;&nbsp;<strong>These comments will not carry through to your database after the code is executed</strong>&nbsp;&#8211; they comment the code only. So, if you comment a file that creates a table, those comments will not appear in the database in any shape or form attached to that table, or otherwise.</p>



<h2 class="wp-block-heading" id="the-comment-statement-for-describing-and-annotating-your-databases">The COMMENT Statement for Describing and Annotating Your Databases</h2>



<p>If you want to annotate your database, so that the comments are included in the database structure for other users connected to, and managing, the database to see, you need to use the&nbsp;<a href="https://www.postgresql.org/docs/current/sql-comment.html" target="_blank" rel="noopener">COMMENT</a>&nbsp;statement. This is a great way to keep your database organised, make sure that everyone working on it (or your future self) knows the purpose of each table, column, function, or other part of your database.</p>



<p>Below, a comment is left on the table tbl_users:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">COMMENT ON TABLE tbl_users IS 'This is the users table.';
</pre>



<p>You can comment on pretty much any entity in a PostgreSQL database:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">COMMENT ON COLUMN tbl_users.email IS 'User email address';
COMMENT ON INDEX user_id IS 'Enforces uniqueness on users ID';
COMMENT ON PROCEDURE report_user_count () IS 'Runs a report on the current user count';
COMMENT ON FUNCTION my_function (timestamp) IS 'Returns the current timestamp';
</pre>



<h2 class="wp-block-heading" id="updating-comments">Updating Comments</h2>



<p>To update a comment, overwrite it:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">COMMENT ON TABLE tbl_users IS 'This comment will overwrite any existing comment on the same entity';
</pre>



<h2 class="wp-block-heading" id="removing-comments">Removing Comments</h2>



<p>To remove a comment, replace it with a null value:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">COMMENT ON TABLE tbl_users IS NULL;</pre>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-comments">How to Comment PostgreSQL Queries/Code, and the PostgreSQL COMMENT Statement</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-comments/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Persist Data When Using PostgreSQL in Docker Containers</title>
		<link>https://www.linuxscrew.com/postgresql-docker-persist-data</link>
					<comments>https://www.linuxscrew.com/postgresql-docker-persist-data#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Sun, 30 Jul 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16396</guid>

					<description><![CDATA[<p>This tutorial will demonstrate how to persist PostgreSQL database data when running PostgreSQL in Docker, by storing the data outside of the container. Docker lets you run PostgreSQL without having to install it directly &#8211; instead keeping it in an isolated container. This is useful if you want to run different versions of PostgreSQL for different projects, or just don&#8217;t want to (or can&#8217;t) install it on your system. The downside of this is that PostgreSQL&#8217;s data is stored inside the container &#8211; if the container ... <a title="How to Persist Data When Using PostgreSQL in Docker Containers" class="read-more" href="https://www.linuxscrew.com/postgresql-docker-persist-data" aria-label="More on How to Persist Data When Using PostgreSQL in Docker Containers">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-docker-persist-data">How to Persist Data When Using PostgreSQL in Docker Containers</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This tutorial will demonstrate how to persist PostgreSQL database data when running PostgreSQL in Docker, by storing the data outside of the container.</p>



<p><a href="https://www.linuxscrew.com/postgresql-docker">Docker lets you run PostgreSQL without having to install it directly</a> &#8211; instead keeping it in an isolated container. This is useful if you want to run different versions of PostgreSQL for different projects, or just don&#8217;t want to (or can&#8217;t) install it on your system.</p>



<p>The downside of this is that PostgreSQL&#8217;s data is stored inside the container &#8211; if the container is destroyed or recreated, the data is lost. If data needs to be kept through these processes, the data must be persisted by storing it outside of the Docker container using one of the below two methods.</p>



<h2 class="wp-block-heading" id="method-1-using-docker-volumes-to-persist-postgresql-data">Method 1: Using Docker Volumes to Persist PostgreSQL Data</h2>



<p><a href="https://docs.docker.com/storage/volumes/" target="_blank" rel="noopener">Docker volumes</a>&nbsp;allow docker containers to store data on the host file system in a separate file. When the container is deleted, the data stored in the volume of it will remain. As Docker volumes are self-contained themselves (representing a file system in a single file), they are portable, encapsulating all of the data within them as a single file that can be mounted in a running docker container.</p>



<p>Here is an example of creating a Docker volume named&nbsp;<em>postgres-data</em>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker volume create postgres-data
</pre>



<p>Now, when you run a Docker PostgreSQL container, you can attach the volume to it, setting the point in the filesystem in the PostgreSQL container where the volume will be mounted:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker run --name postgres_server -v postgres-data:/var/lib/postgresql/data -d postgres
</pre>



<p>This will create a container named postgres_server and mount the&nbsp;<em>postgres-data</em>&nbsp;volume to the&nbsp;<em>/var/lib/postgresql/data</em>&nbsp;directory within the container using the&nbsp;<strong>-v</strong>&nbsp;option. When PostgreSQL writes to that directory, the data will be written to the volume outside of the container, persisting it.</p>



<p>Docker volumes can be listed by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker volume ls
</pre>



<p>This is the preferred method for persisting data in Docker if you are looking for the simplest, easiest the manage solution.</p>



<h2 class="wp-block-heading" id="method-2-using-docker-bind-mounts-to-persist-postgresql-data">Method 2: Using Docker Bind Mounts to Persist PostgreSQL Data</h2>



<p>If you want to store your PostgreSQL data outside of Docker completely, and be in full control of it for the purposes of backing it up or moving it about, this alternative method for persisting PostgreSQL data in Docker works well.</p>



<p><a href="https://docs.docker.com/storage/bind-mounts/" target="_blank" rel="noopener">Docker bind mounts</a>&nbsp;mount a directory from the host directly to one inside a running Docker container. Changes made in the directory by either the host or the container will be reflected in each &#8211; they will be the same.</p>



<p>To create a bind mount, first create the directory on the host that will be mapped to the directory inside the container:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mkdir /path/to/postgres/data
</pre>



<p>Choose a path that the docker service will be able to write to.</p>



<p>Now, when launching a Docker container, the bind mount can be specified:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker run --name postgres_server -v /path/to/postgres/data:/var/lib/postgresql/data -d postgres
</pre>



<p>Like volumes, bind mounts uses the&nbsp;<strong>-v</strong>&nbsp;option. Above, the path&nbsp;<em>/path/to/postgres/data</em>&nbsp;on the host is mapped to&nbsp;<em>/var/lib/postgresql/data</em>&nbsp;inside the container. When data is written to&nbsp;<em>/var/lib/postgresql/data</em>&nbsp;by PostgreSQL in the container, it is also written to&nbsp;<em>/var/lib/postgresql/data</em>&nbsp;on the host machine, meaning that it will be available if the container is destroyed.</p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-docker-persist-data">How to Persist Data When Using PostgreSQL in Docker Containers</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-docker-persist-data/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Drop/Delete/Destroy a Database in PostgreSQL</title>
		<link>https://www.linuxscrew.com/postgresql-drop-database</link>
					<comments>https://www.linuxscrew.com/postgresql-drop-database#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Sat, 29 Jul 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16412</guid>

					<description><![CDATA[<p>This short tutorial will show you how to completely delete a database from PostgreSQL, and provide code examples. Before you Delete a Database&#8230; There are a few things you should do before you try and delete a PostgreSQL database,&#160;especially&#160;if you&#8217;re working on a production server. First,&#160;take a backup of your PostgreSQL server&#160;&#8211; just in case you delete the wrong thing, or change your mind later. Next, make sure you&#8217;re logged in as the&#160;default postgresql admin user, or a user with permission to delete databases. And ... <a title="How to Drop/Delete/Destroy a Database in PostgreSQL" class="read-more" href="https://www.linuxscrew.com/postgresql-drop-database" aria-label="More on How to Drop/Delete/Destroy a Database in PostgreSQL">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-drop-database">How to Drop/Delete/Destroy a Database in PostgreSQL</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This short tutorial will show you how to completely delete a database from PostgreSQL, and provide code examples.</p>



<h2 class="wp-block-heading" id="before-you-delete-a-database">Before you Delete a Database&#8230;</h2>



<p>There are a few things you should do before you try and delete a PostgreSQL database,&nbsp;<em>especially</em>&nbsp;if you&#8217;re working on a production server.</p>



<p>First,&nbsp;<a href="https://www.linuxscrew.com/postgresql-backup-import-export">take a backup of your PostgreSQL server</a>&nbsp;&#8211; just in case you delete the wrong thing, or change your mind later.</p>



<p>Next, make sure you&#8217;re logged in as the&nbsp;<a href="https://www.linuxscrew.com/postgresql-default-password-reset">default postgresql admin user</a>, or a user with permission to delete databases.</p>



<p>And finally, make sure you&#8217;re sure of the name of the database you want to delete, and that there&#8217;s nothing you need to keep in it, and that no other databases or systems are relying on it. Deleting cannot be undone (unless you have a backup to restore).</p>



<h2 class="wp-block-heading" id="deletingdroppingdestroying-a-database-in-postgresql-with-the-drop-database-statement">Deleting/Dropping/Destroying a Database in PostgreSQL with the DROP DATABASE Statement</h2>



<p>The PostgreSQL&nbsp;<a href="https://www.postgresql.org/docs/current/sql-dropdatabase.html" target="_blank" rel="noopener">DROP DATABASE</a>&nbsp;statement is used to delete a database by name. It&#8217;s as easy to use as:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">DROP DATABASE database_name;
</pre>



<p>The database,&nbsp;<strong>including all tables, functions, and other data in it</strong>&nbsp;will be deleted.</p>



<h2 class="wp-block-heading" id="checking-that-the-database-exists-before-attempting-deletion">Checking that the Database Exists Before Attempting Deletion</h2>



<p>If the database you are trying to delete doesn&#8217;t exist, you&#8217;ll get an error. This can be avoided by only deleting the database if it exists:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">DROP DATABASE IF EXISTS database_name;
</pre>



<p>Almost&nbsp;<em>too</em>&nbsp;easy!</p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-drop-database">How to Drop/Delete/Destroy a Database in PostgreSQL</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-drop-database/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Run PostgreSQL in a Docker Container in Linux</title>
		<link>https://www.linuxscrew.com/postgresql-docker</link>
					<comments>https://www.linuxscrew.com/postgresql-docker#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Fri, 28 Jul 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16401</guid>

					<description><![CDATA[<p>This article will demonstrate how to run the PostgreSQL database service in a Docker container. Docker lets you run software in isolated environments that encapsulate specific configurations, software versions and dependencies separate from your main system (and each other). This makes it very useful for running multiple versions of the same program. There are many other reasons why you may wish to run PostgreSQL in a container &#8211; from ensuring consistency between systems, and running a version of PostgreSQL that is not available in your ... <a title="How to Run PostgreSQL in a Docker Container in Linux" class="read-more" href="https://www.linuxscrew.com/postgresql-docker" aria-label="More on How to Run PostgreSQL in a Docker Container in Linux">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-docker">How to Run PostgreSQL in a Docker Container in Linux</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This article will demonstrate how to run the PostgreSQL database service in a Docker container. Docker lets you run software in isolated environments that encapsulate specific configurations, software versions and dependencies separate from your main system (and each other). This makes it very useful for running multiple versions of the same program. There are many other reasons why you may wish to run PostgreSQL in a container &#8211; from ensuring consistency between systems, and running a version of PostgreSQL that is not available in your package manager.</p>



<h2 class="wp-block-heading" id="installing-docker">Installing Docker</h2>



<p>To use Docker, you&#8217;ll need to install it on your Linux system,&nbsp;<a href="https://www.linuxscrew.com/linux-install-docker">as we detail in our instructions here</a>.</p>



<h2 class="wp-block-heading" id="running-a-postgresql-database-server-in-docker">Running a PostgreSQL Database Server in Docker</h2>



<p>Once you have installed Docker, you can start pulling images (downloading the image from an image repository). Images are by default pulled from Docker&#8217;s public repository on&nbsp;<a href="https://hub.docker.com/search?q=" target="_blank" rel="noopener">Docker Hub</a>.</p>



<p>Docker has an official&nbsp;<a href="https://hub.docker.com/_/postgres" target="_blank" rel="noopener">PostgreSQL image</a>&nbsp;which can be pulled by running:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker pull postgres
</pre>



<h3 class="wp-block-heading" id="pulling-a-specific-version-of-postgresql">Pulling a Specific Version of PostgreSQL</h3>



<p>Docker images are organized by&nbsp;<em>tags</em>&nbsp;&#8211; labels that usually include the version of the primary software in the container, or other notable attributes of it such as additional packages that may be included.</p>



<p>By default, pulling an image will pull the most recent version, tagged&nbsp;<em>latest</em>.</p>



<p>To install a specific version of PostgreSQL, find the image on Docker Hub and click on the &#8216;<a href="https://hub.docker.com/_/postgres?tab=tags" target="_blank" rel="noopener">View Available Tags</a>&#8216; button &#8211; you&#8217;ll get a full list of available images for all versions of PostgreSQL, and the name of the image for that version. For example, to install PostgreSQL 15 specifically, run:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker pull postgres:15
</pre>



<h2 class="wp-block-heading" id="running-a-docker-image-after-pulling-it">Running a Docker Image After Pulling It</h2>



<p>Once the PostgreSQL image has been pulled (again, Docker lingo for&nbsp;<em>downloaded</em>)m it can be run using the following command:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker run --name SERVER_NAME -e POSTGRES_PASSWORD=USER_PASSWORD -d postgres
</pre>



<p>Note that the following values should be replaced:</p>



<ul><li><strong>SERVER_NAME</strong>&nbsp;is a unique name for identifying running the PostgreSQL server container</li><li><strong>USER_PASSWORD</strong>&nbsp;should be the password for the&nbsp;<em>postgres</em>&nbsp;user which will be used to connect to the server</li></ul>



<p>More information can be found in the&nbsp;<a href="https://www.postgresql.org/docs/14/app-initdb.html" target="_blank" rel="noopener">PostgreSQL documentation</a>.</p>



<h2 class="wp-block-heading" id="connecting-to-the-postgresql-server-running-in-docker">Connecting to the PostgreSQL Server Running in Docker</h2>



<p>By default, the container will map it&#8217;s internal PostgreSQL port to the default PostgreSQL port on the local system (5432). Connect using the (psql command)[<a href="https://www.linuxscrew.com/postgresql-how-to-connect">https://www.linuxscrew.com/postgresql-how-to-connect</a>]:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">psql -h localhost -p 5432 -U postgres
</pre>



<h2 class="wp-block-heading" id="stopping-and-removing-docker-containers">Stopping and Removing Docker Containers</h2>



<p>Once you have finished with your database, the container can be stopped:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker stop postgres
</pre>



<p>If you want to free up resources, you can also completely delete/destroy the container.&nbsp;<strong>WARNING, THIS WILL REMOVE ANY DATA YOU STORED IN THE DATABASES HOSTED ON THE POSTGRESQL SERVER WITHIN THE CONTAINER</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="bash" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">docker rm postgres
</pre>



<h2 class="wp-block-heading" id="where-is-my-data-stored-when-working-with-docker-containers">Where is My Data Stored When Working With Docker Containers?</h2>



<p>By default, data (including PostgreSQL databases and their contents) are stored inside the container, and will be deleted with it if it is removed or recreated.</p>



<p>If you want your data to persist after removing a docker container, you will need to configure it to store data&nbsp;<em>outside of the container</em>&nbsp;&#8211;&nbsp;<a href="https://www.linuxscrew.com/postgresql-docker-persist-data">we cover how to do this in our article here</a>.</p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-docker">How to Run PostgreSQL in a Docker Container in Linux</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-docker/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Drop/Delete/Destroy a Table Column in PostgreSQL</title>
		<link>https://www.linuxscrew.com/postgresql-drop-column</link>
					<comments>https://www.linuxscrew.com/postgresql-drop-column#respond</comments>
		
		<dc:creator><![CDATA[Brad Morton]]></dc:creator>
		<pubDate>Thu, 27 Jul 2023 00:00:00 +0000</pubDate>
				<category><![CDATA[Databases]]></category>
		<guid isPermaLink="false">https://www.linuxscrew.com/?p=16407</guid>

					<description><![CDATA[<p>This quick tutorial will show you how to delete/destroy/drop a table column in a PostgreSQL database. Before you Delete a Table in your Database&#8230; There are a few things you should do before you try and delete a table from your PostgreSQL database,&#160;especially&#160;if you&#8217;re working with data in production. First,&#160;take a backup of your PostgreSQL server&#160;&#8211; just in case you delete the wrong thing, or change your mind later. Next, make sure you&#8217;re logged in as the&#160;default postgresql admin user, or a user with permission ... <a title="How to Drop/Delete/Destroy a Table Column in PostgreSQL" class="read-more" href="https://www.linuxscrew.com/postgresql-drop-column" aria-label="More on How to Drop/Delete/Destroy a Table Column in PostgreSQL">Read more</a></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-drop-column">How to Drop/Delete/Destroy a Table Column in PostgreSQL</a></p>
]]></description>
										<content:encoded><![CDATA[
<p>This quick tutorial will show you how to delete/destroy/drop a table column in a PostgreSQL database.</p>



<h2 class="wp-block-heading" id="before-you-delete-a-table-in-your-database">Before you Delete a Table in your Database&#8230;</h2>



<p>There are a few things you should do before you try and delete a table from your PostgreSQL database,&nbsp;<em>especially</em>&nbsp;if you&#8217;re working with data in production.</p>



<p>First,&nbsp;<a href="https://www.linuxscrew.com/postgresql-backup-import-export">take a backup of your PostgreSQL server</a>&nbsp;&#8211; just in case you delete the wrong thing, or change your mind later.</p>



<p>Next, make sure you&#8217;re logged in as the&nbsp;<a href="https://www.linuxscrew.com/postgresql-default-password-reset">default postgresql admin user</a>, or a user with permission to delete the table in question.</p>



<p>And finally, make sure you&#8217;re sure of the name of the column (and the table it&#8217;s in) that you want to delete, and that there&#8217;s nothing you need to keep in it, and that no other objects in your database are relying on it. Deleting cannot be undone (unless you have a backup to restore).</p>



<h2 class="wp-block-heading" id="deleting-a-column-from-a-postgresql-table-using-drop-column">Deleting a Column from a PostgreSQL Table Using DROP COLUMN</h2>



<p>To delete a table column in a PostgreSQL database you will need to use the&nbsp;<a href="https://www.postgresql.org/docs/current/sql-altertable.html" target="_blank" rel="noopener">ALTER TABLE and DROP COLUMN statements</a>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ALTER TABLE table_name DROP COLUMN column_name;
</pre>



<p>Multiple columns can be deleted at the same time by listing them, separated by a comma:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ALTER TABLE table_name DROP COLUMN column_1_, column_2;
</pre>



<h2 class="wp-block-heading" id="deleting-a-column-only-if-it-exists">Deleting a Column Only if it Exists</h2>



<p>An error will be returned if you try to delete a column that does not exist. This can be avoided by checking whether the column exists before deleting it:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ALTER TABLE table_name DROP COLUMN IF EXISTS column_name;
</pre>



<h2 class="wp-block-heading" id="deleting-a-column-and-all-associated-objects">Deleting a Column and All Associated Objects</h2>



<p>If you try to delete a column that is referenced by other objects in the database, an error will be received. To delete a column and all objects that are associated with it, add the CASCADE statement:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ALTER TABLE table_name DROP COLUMN column_name CASCADE;
</pre>



<p>Be careful when doing this, and make sure you&#8217;re aware of exactly what objects rely on the column being deleted, as they will be removed, as will other reliant entities and any data they contain.</p>



<h2 class="wp-block-heading" id="safely-deleting-columns-with-restrict">Safely Deleting Columns with RESTRICT</h2>



<p>To remove a column only if there are no foreign keys or other objects in PostgreSQL that rely on it, you can use the RESTRICT statement:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="postgresql" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ALTER TABLE table_name DROP COLUMN column_name RESTRICT;
</pre>



<p>If the check fails, the column deletion will fail. Note that this is now the default behaviour in PostgreSQL.</p>



<p><strong>Remember, when a table column is removed, so is all of the data in it!</strong></p>
<p>View the original article from LinuxScrew here: <a rel="nofollow" href="https://www.linuxscrew.com/postgresql-drop-column">How to Drop/Delete/Destroy a Table Column in PostgreSQL</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.linuxscrew.com/postgresql-drop-column/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
