{"id":107518,"date":"2025-08-04T13:14:06","date_gmt":"2025-08-04T13:14:06","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=107518"},"modified":"2025-08-04T13:14:07","modified_gmt":"2025-08-04T13:14:07","slug":"using-the-psql-command-line-utility-to-work-with-azure-database-for-postgresql-flexible-server","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/using-the-psql-command-line-utility-to-work-with-azure-database-for-postgresql-flexible-server\/","title":{"rendered":"How to Use the\u00a0psql\u00a0Command-Line Tool with Azure Database for PostgreSQL Flexible Server"},"content":{"rendered":"\n<p>A number of client tools are available for working with <a href=\"https:\/\/learn.microsoft.com\/en-us\/azure\/postgresql\/flexible-server\/overview\" target=\"_blank\" rel=\"noreferrer noopener\">Azure Database for PostgreSQL flexible server<\/a>. With these tools, you can connect to your ADP instance, manage the environment, and define your database structures. You can also query, update and delete data within those databases. Two of the most popular tools for working with <a href=\"https:\/\/www.postgresql.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL<\/a> are the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/postgresql-basics-getting-started-with-psql\/\" target=\"_blank\" rel=\"noreferrer noopener\">psql command-line utility<\/a> and the <a href=\"https:\/\/www.pgadmin.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">pgAdmin<\/a> GUI management tool, both of which are available for free on multiple platforms.<\/p>\n\n\n\n<p>In this article, I introduce you to psql and show you how to connect to and work with your ADP instance. This article is the second in a series on ADP flexible server: in the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/getting-started-with-azure-database-for-postgresql-flexible-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">first article<\/a>, I introduced you to ADP and provided information to help you get started with your own ADP instance, and in <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/using-pgadmin-to-work-with-azure-database-for-postgresql-flexible-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">part three<\/a> l introduce you to pgAdmin, so you know how to use both that <em>and<\/em> psql.<\/p>\n\n\n\n<p>Having a foundation in these two tools will make it easier to understand the different concepts I introduce in these articles, so you can immediately start using the tools to interface with your ADP instance. This foundation will also help you down the road when working with production PostgreSQL deployments, and if you need to carry out administrative tasks or access the data.<\/p>\n\n\n\n<p>With all that in mind, let\u2019s start diving into the psql utility. It provides a client interface for working with PostgreSQL from your local <a href=\"https:\/\/learn.microsoft.com\/en-us\/powershell\/utility-modules\/aishell\/concepts\/what-is-a-command-shell?view=ps-modules\" target=\"_blank\" rel=\"noreferrer noopener\">command shell<\/a> environment. You can use psql in interactive mode, which enables you to run SQL statements or other commands interactively. You can also use psql without entering interactive mode to carry out one-off tasks such as running scripts or querying a database. You\u2019ll get a better sense of how all this works as we work through the article.<\/p>\n\n\n\n<p><em><strong>Note<\/strong>: to follow along with the examples in this article, you need to have a running ADP instance and know the necessary connection information, including the full endpoint name, port number, user name, password, and target database. If you haven\u2019t set up ADP yet, refer to the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/getting-started-with-azure-database-for-postgresql-flexible-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">first article<\/a>.<\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"h-how-to-install-psql-on-your-system\">How to install psql on your system<\/h1>\n\n\n\n<p>Before you can use psql, it must be installed on your system. The utility is typically included with a PostgreSQL installation, so if you\u2019ve set up PostgreSQL, you\u2019re probably all set. You should be able to run psql in your system\u2019s <a href=\"https:\/\/aws.amazon.com\/what-is\/cli\/#:~:text=A%20command%20line%20interface%20(CLI)%20is%20a%20text%2Dbased,with%20a%20computer's%20operating%20system.\" target=\"_blank\" rel=\"noreferrer noopener\">command-line interface (CLI)<\/a>, such as Terminal in macOS or Ubuntu. You can verify whether psql is installed by executing the following command at the CLI command prompt:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql --version<\/pre><\/div>\n\n\n\n<p>If psql is installed, the command will return the version number. If it is not installed, the command will return a message indicating so. The message might even suggest how to install it.<\/p>\n\n\n\n<p>You can install psql on macOS, Linux and Windows computers. However, the installation process varies between platforms. A platform might also support multiple installation methods. You might have to do some digging to find information specific to your environment. We have <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/postgresql-basics-getting-started-with-psql\/\" target=\"_blank\" rel=\"noreferrer noopener\">this article<\/a> here on Simple Talk, and I also recommend <a href=\"https:\/\/www.timescale.com\/blog\/how-to-install-psql-on-mac-ubuntu-debian-windows\" target=\"_blank\" rel=\"noreferrer noopener\">this article<\/a> from Timescale.<\/p>\n\n\n\n<p>I installed psql on macOS, Ubuntu and Windows systems. On my Mac, I installed the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/libpq.html\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL libpq library<\/a>, which includes the psql utility. The library is one of the components included in the PostgreSQL database platform. Because I did not need the entire platform, I installed only the libpq library by running the following <a href=\"https:\/\/brew.sh\/\" target=\"_blank\" rel=\"noreferrer noopener\">Homebrew<\/a> command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >brew install libpq<\/pre><\/div>\n\n\n\n<p>After I installed the libpq library on my Mac, I ran the following brew command to create a symlink to the library\u2019s binaries so I could easily run the psql utility:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >brew link --force libpq<\/pre><\/div>\n\n\n\n<p>I took a similar approach on Ubuntu, except that I used the apt package manager to install the postgresql-client client library, which contains psql and other tools. The following sudo command shows how I installed the library on Ubuntu:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >sudo apt install postgresql-client<\/pre><\/div>\n\n\n\n<p>With Windows, I took a much different approach. First, I downloaded the <a href=\"https:\/\/www.enterprisedb.com\/downloads\/postgres-postgresql-downloads\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL installer certified by EDB<\/a> and then ran the installer\u2019s <strong>Setup<\/strong> wizard. When I got to the wizard\u2019s <strong>Select Components<\/strong> screen, I selected only the <strong>Command Line Tools<\/strong> option, as shown in the following figure. <em>(I choose not to install any of the other components).<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"815\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1024x815.png\" alt=\"The wizard's 'Select Components' screen, with 'Command Line Tools' selected.\" class=\"wp-image-107519\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1024x815.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-300x239.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-768x611.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image.png 1090w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>For the rest of the wizard, I stuck with the default settings. You can find more information about installing the Windows-based client tools in the PostgreSQL topic <a href=\"https:\/\/www.postgresql.org\/download\/windows\/\" target=\"_blank\" rel=\"noreferrer noopener\">Windows installers<\/a>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"h-connecting-to-azure-database-for-postgresql-with-psql\">Connecting to Azure Database for PostgreSQL with psql<\/h1>\n\n\n\n<p>After you install psql, you can start using it to work with your ADP instance. Before you do that, be sure that the instance is running and you\u2019ve added your local IP address to the networking settings (as described in the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/getting-started-with-azure-database-for-postgresql-flexible-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">first article<\/a>). You can also use psql to connect to other PostgreSQL deployments, but for now, our concern is primarily with ADP.<\/p>\n\n\n\n<p>When you connect to an ADP instance, you must provide the following connection information:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><strong>Host server.<\/strong> The fully qualified endpoint name that Azure assigns to your instance when you create it. For my instance, Azure assigned the name adpserver.postgres.database.azure.com.<\/li>\n\n\n\n<li><strong>Port.<\/strong> ADP flexible server uses TCP port 5432.<\/li>\n\n\n\n<li><strong>User name.<\/strong> You can use either the PostgreSQL account that you set up when fist creating your ADP instance, or you can use your <a href=\"https:\/\/www.microsoft.com\/en-gb\/security\/business\/microsoft-entra\" target=\"_blank\" rel=\"noreferrer noopener\">Microsoft Entra<\/a> account. For the examples in this article, I used my PostgreSQL account, which is adpadmin.<\/li>\n\n\n\n<li><strong>Password.<\/strong> The password associated with the specified user account.<\/li>\n\n\n\n<li><strong>Database.<\/strong> The database that will be active when you first connect to your ADP instance. You can change to a different database at any time after you\u2019ve connected to ADP. For now, we\u2019ll stick with the postgres user database, which Azure creates automatically when you set up your ADP instance.<\/li>\n<\/ul>\n<\/div>\n\n\n<p>To help you connect to an ADP instance, the Azure portal page for that instance provides connection information for different platforms. In the left panel, expand <strong>Settings<\/strong> and click the <strong>Connect<\/strong> node. The main window will then display connection information.<\/p>\n\n\n\n<p>You can run psql in a command shell such as Bash or Zsh. When you run the utility, you must provide the connection information either directly (as part of the command) or indirectly (through a configuration file). To provide the information directly, include the connection information as arguments in the psql command, as in the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql -h adpserver.postgres.database.azure.com -p 5432 -U adpadmin -d postgres<\/pre><\/div>\n\n\n\n<p>This is the command that I ran in Zsh on my Mac. If you copy this command for your connection, be sure to replace the host name and user name with your specific information. Here\u2019s a breakdown of the command\u2019s arguments:<\/p>\n\n\n<div class=\"block-core-list\">\n<ul class=\"wp-block-list\">\n<li><strong>-h.<\/strong> Host server instance<\/li>\n\n\n\n<li><strong>-p.<\/strong> Port for the ADP service<\/li>\n\n\n\n<li><strong>-U.<\/strong> User name<\/li>\n\n\n\n<li><strong>-d.<\/strong> Target ADP database<\/li>\n<\/ul>\n<\/div>\n\n\n<p>When you run the psql command, you\u2019ll be prompted for the password associated with the specified user account. If everything checks out, you\u2019ll be connected to your ADP instance. The command shell will display connection information that confirms your connection. The command prompt will also change to the name of the active database, in this case, postgres. The database command prompt indicates that you are now in interactive mode. The following figure shows the terminal after I connected to my ADP instance:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"392\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1-1024x392.png\" alt=\"The terminal after connecting to the ADP instance.\" class=\"wp-image-107520\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1-1024x392.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1-300x115.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1-768x294.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-1.png 1090w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>At the psql command prompt, you can enter both SQL statements and meta-commands that target the ADP instance. (I\u2019ll be discussing both of these shortly).<\/p>\n\n\n\n<p>For example, the following meta-command lists all the current databases on your ADP instance:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\l<\/pre><\/div>\n\n\n\n<p>The command returns details about the existing databases, such as the owner, encoding, collation, and access privileges. You might recall from the<a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/getting-started-with-azure-database-for-postgresql-flexible-server\/\" target=\"_blank\" rel=\"noreferrer noopener\"> first article<\/a> that the Azure portal lists only three databases after you first create your instance: azure_maintenance, azure_sys, and postgres. However, the psql \\l command also returns the template0 and template1 databases, which are used for creating new databases &#8211; more on that in a future article.<\/p>\n\n\n\n<p>By running the \\l command, you\u2019re able to quickly confirm that psql can properly connect to the ADP instance and retrieve the requested information, which is all you need to do for now. We\u2019ll be covering more commands later in the article. In the meantime, you should quit psql by running the following command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\/q<\/pre><\/div>\n\n\n\n<p>When you quit psql, you\u2019re returned to the regular command prompt. You can relaunch psql at any time. Before you do, however, let\u2019s look at how you can simplify your connections going forward.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Adding connection settings to a configuration file<\/h1>\n\n\n\n<p>As I noted in the previous section, you must provide the connection information to your psql command either directly or indirectly when connecting to your ADP instance. You\u2019ve already seen how to create a direct connection. Now let\u2019s look at how to set up an indirect connection.<\/p>\n\n\n\n<p>In psql, you can use any of the supported environment variables to define connection information. This way, you do not have to include this information in your psql commands &#8211; instead, you add the variables and their values to the startup configuration file specific to your command shell. For example, I use the Zsh command shell on my Mac, so I updated the <strong>.zshrc<\/strong> configuration file in my home directory. Zsh reads the file when first starting up.<\/p>\n\n\n\n<p>To support my psql connections, I added the following settings to the file:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >export PGHOST=adpserver.postgres.database.azure.com\nexport PGPORT=5432\nexport PGUSER=adpadmin\nexport PGDATABASE=postgres\n<\/pre><\/div>\n\n\n\n<p>I did something similar in Ubuntu, which uses the Bash command shell by default. In this case, I updated the <strong>.bashrc<\/strong> file in my home directory.<\/p>\n\n\n\n<p>Whenever you add or update environment variables for psql, you must quit your CLI if it\u2019s still running and then restart it. After you restart the shell, you can verify the variable settings by running the following echo command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >echo $PGHOST, $PGPORT, $PGUSER, $PGDATABASE<\/pre><\/div>\n\n\n\n<p>The command should return the four variable values. For more information about the environment variables available to psql, see the PostgreSQL topic <a href=\"https:\/\/www.postgresql.org\/docs\/current\/libpq-envars.html\" target=\"_blank\" rel=\"noreferrer noopener\">Environment Variables<\/a>. The topic also includes information about setting up the environment variables on Windows.<\/p>\n\n\n\n<p>Note that psql also supports the <code>PGPASSWORD<\/code> environment variable, which you can add to your configuration file along with the others. However, PostgreSQL documentation recommends against including this variable for security reasons, which means that you\u2019ll still be prompted for a password when you run your psql command.<\/p>\n\n\n\n<p>If you want to avoid being prompted for a password but do not want to use the <code>PGPASSWORD<\/code> environment variable, you can instead add a password file to your system. In macOS or Linux, you would create the <strong>.pgpass<\/strong> file in your home directory and include your password in there. If working in Windows, you\u2019ll need to create the <strong>%APPDATA%\\postgresql\\pgpass.conf<\/strong> file.<\/p>\n\n\n\n<p>By using a separate password file, you can set stricter restrictions than you can with the command shell\u2019s startup configuration file. In fact, psql will inform you if the file\u2019s permissions are not considered restrictive enough when you try to connect to your ADP instance.<\/p>\n\n\n\n<p>To set up the <strong>.pgpass<\/strong> or <strong>pgpass.conf<\/strong> file, create an empty text file and add the following connection string:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >*:*:*:*:&lt;password&gt;<\/pre><\/div>\n\n\n\n<p>The first four asterisk wildcards represent the hostname, port, database, username, and password. By using asterisks for these components, you can apply the password to any connection. However, you can be much more specific by including these components, which can be handy when working with multiple PostgreSQL deployments.<\/p>\n\n\n\n<p><em><strong>Note<\/strong>: including the other components in your connection string does not replace the need for providing the environment variables in the startup configuration file. Except for the password itself, the connection information in the <strong>.pgpass<\/strong> or <strong>pgpass.conf<\/strong> file is used only for matching purposes. For more information about the password file, refer to the PostgreSQL topic <a href=\"https:\/\/www.postgresql.org\/docs\/current\/libpq-pgpass.html\" target=\"_blank\" rel=\"noreferrer noopener\">The Password File<\/a>.<\/em><\/p>\n\n\n\n<p>After you\u2019ve added the connection string to the password file, be sure to restart your CLI again. If you\u2019ve configured all the settings correctly, you should now be able to launch psql and connect to your ADP instance simply by running the following command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql<\/pre><\/div>\n\n\n\n<p>That\u2019s all there is to it. From here on in, you should be able to access your ADP instance with this simple psql command. Once you have psql running, you can run a wide range of SQL statements and meta-commands in interactive mode.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Running SQL commands in psql<\/h1>\n\n\n\n<p>PostgreSQL is a relational database management system. As such, it supports the type of SQL statements you\u2019ve seen in systems such as <a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL<\/a> or <a href=\"https:\/\/www.microsoft.com\/en-gb\/sql-server\/sql-server-downloads\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server<\/a>, with the usual product-specific adaptations you might expect. You can run SQL statements at the psql command prompt, making it possible to define schema, create indexes, manipulate data, and carry out other actions.<\/p>\n\n\n\n<p>To run a SQL statement, you simply type the statement and press enter. The command is sent to your ADP instance, which processes the statement and, if appropriate, returns data, a message, or both. If any information is returned, psql will display it beneath the original statement. For example, the following <code>CREATE TABLE<\/code> statement creates a table named tbl in the postgres database:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >CREATE TABLE tbl (col1 smallint, col2 text);<\/pre><\/div>\n\n\n\n<p>When you run this command, your ADP instance will add the table to the database and return a <code>CREATE TABLE<\/code> message. Notice that the statement ends with a semi-colon. You must include the semi-colon so the database engine knows when it has the complete SQL statement. This makes it possible to submit multi-line code as a single statement, as in the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >INSERT INTO tbl \nvalues \n  (101, 'abc'),\n  (102, 'def'),\n  (103, 'ghi'),\n  (104, 'jkl'),\n  (105, 'mno');\n<\/pre><\/div>\n\n\n\n<p>The <code>INSERT<\/code> statement adds five rows to the tbl table and returns the message <code>INSERT 0 5<\/code>. The first number, which is always 0, is a carryover from when the database engine used to return an object ID (OID). The OID was related to the number of returned rows. However, this feature is no longer supported, although the 0 remains. The second number, 5, indicates the number of inserted rows.<\/p>\n\n\n\n<p>When you enter a multi-line SQL statement in your shell, the command prompt itself changes slightly. The equal sign that follows the database name becomes a dash after the first line. This indicates that a multi-line statement is being submitted to the database engine and should be processed in its entirety, up to the final semi-colon. The following figure shows the statement across the multiple lines. As you can see, the command prompt is different for the lines after the first:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-2-1024x584.png\" alt=\"The statement across the multiple lines.\" class=\"wp-image-107521\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-2-1024x584.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-2-300x171.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-2-768x438.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-2.png 1090w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can also run SQL statements that return data, just like you\u2019d expect with any client that issues SQL statements. For example, the following <code>SELECT<\/code> statement returns all rows in the tbl table:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >SELECT * FROM tbl;<\/pre><\/div>\n\n\n\n<p>The data is returned in a tabular format, as shown in the following figure. Notice that the number of returned rows (in parentheses) is also included with the results:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"579\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-3.png\" alt=\"The data, returned in a tabular format.\" class=\"wp-image-107522\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-3.png 635w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-3-300x274.png 300w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/figure>\n\n\n\n<p>Running SQL statements against your ADP instance is a fairly straightforward process. You can make those statements as simple or as complex as necessary. As you\u2019ll see in the next section, you can also run <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/postgresql-basics-essential-psql-tips-and-tricks\/#essential-meta-commands\" target=\"_blank\" rel=\"noreferrer noopener\">meta-commands<\/a>, which can help with administration and scripting.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Running meta-commands in psql<\/h1>\n\n\n\n<p>Meta-commands in psql enable you to retrieve information, configure settings, import data, set variables, and perform other operations. A meta-command always begins with a backslash, followed by the command name, which might be nothing but a single letter. Many meta-commands also take arguments, and some arguments take values.<\/p>\n\n\n\n<p>You already saw a couple of meta-commands earlier in the article, when you first connected to your ADP instance. As you\u2019ll recall, you used the \\l command to list the current databases and the \\q command to quit the psql program. In some cases, as with these two commands, a command might support two variations. For example, you can use the \\list command instead of the \\l command, and you can use the \\quit command instead of the \\q command.<\/p>\n\n\n\n<p>Now let\u2019s look at another way to use the \\l command. In the following example, the command includes post* as an argument:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\l post*<\/pre><\/div>\n\n\n\n<p>The asterisk is used as a wildcard, which means that psql will return information about any database whose name begins with post, followed by zero or more characters. In this case, only the postgres database matches the search pattern, but the results could have included databases with names such as post08, post_test, postprod, or even just post.<\/p>\n\n\n\n<p>Another meta-command you might find useful is \\dt. The command returns information about the tables defined in the current database, as shown in the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\dt<\/pre><\/div>\n\n\n\n<p>The root command in this case is \\d, which takes many forms. For example, \\d alone returns information about the database\u2019s tables, indexes, views, materialized views, sequences, and other objects. If t is added, the returned information is limited to tables. Other examples include \\di for indexes, \\ds for sequences, and \\dm for materialized views.<\/p>\n\n\n\n<p>You can also use the \\d command to limit the returned information to a specific object. For example, the following command returns information specific to the tbl table:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\d tbl<\/pre><\/div>\n\n\n\n<p>Meta-commands are quite varied in the types of operations they support. You can even use a meta-command to control the borders that surround the result set returned by a query. For instance, the following command changes the border from its default (shown in the previous figure) to one that better distinguishes the results:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\pset border 2<\/pre><\/div>\n\n\n\n<p>The command starts with \\pset, which lets you set options affecting the query output. This is followed the border argument and its value, 2. A value of 0 sets the output to no border, and a 1 creates internal dividing lines, which is the default. A value of 2 is used to create a complete table frame around the results.<\/p>\n\n\n\n<p>You can see the effects of the \\pset border setting by running the following <code>SELECT<\/code> statement, which returns all rows from the tbl table:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >SELECT * FROM tbl;<\/pre><\/div>\n\n\n\n<p>The following figure shows the statement and its returned values. Notice that the returned data is now completely surrounded by borders:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"677\" height=\"773\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-4.png\" alt=\"The statement and its returned values.\" class=\"wp-image-107523\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-4.png 677w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-4-263x300.png 263w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/figure>\n\n\n\n<p>To set the border back to the default value, you can run the following \\pset command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\pset border 1 <\/pre><\/div>\n\n\n\n<p>These are just a few examples of the many operations you can perform with meta-commands. I recommend that you review the <a href=\"https:\/\/www.postgresql.org\/docs\/current\/app-psql.html\" target=\"_blank\" rel=\"noreferrer noopener\">psql documentation<\/a> to better understand your options.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Outputting query results to a file<\/h1>\n\n\n\n<p>In some cases, you might want to output your query results to a file. You can do this by using the \\o or \\out command. When you want to control output, you must specifically tell psql to start outputting the data and then tell it to stop. For example, the following \\o command tells psql to start outputting the results to the <strong>psql_out.txt<\/strong> file:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\o psql_out.txt<\/pre><\/div>\n\n\n\n<p>When you run the command, psql creates an empty file named <strong>psql_out.txt<\/strong>. After that, any data returned by your statements is added to the file until you specifically stop the process. You can try this out by running the following <code>SELECT<\/code> statement:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >SELECT col1, col2 FROM tbl \nWHERE col1 IN (102, 103, 104);\n<\/pre><\/div>\n\n\n\n<p>When you run this statement, psql adds the query results to the <strong>psql_out.txt<\/strong> file. In addition, it does not display any data or message on the screen. You can confirm that the data has been added to the file by viewing the file\u2019s contents. To stop outputting the data to the file, simply run the following \\o command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >\\o<\/pre><\/div>\n\n\n\n<p>If you were to now rerun your <code>SELECT<\/code> statement, the results would again be returned to the screen, and nothing more would be added to the file.<\/p>\n\n\n\n<p>With that in mind, let\u2019s quit the psql program by running the \\q command. This will return you to your regular command prompt, where we can work with psql in non-interactive mode.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Working with psql in non-interactive mode<\/h1>\n\n\n\n<p>You can use psql without entering interactive mode. For example, you might want to run a SQL script against your ADP instance but don\u2019t need to interface with your ADP instance beyond that. In this case, all you have to do is call the file as part of your psql command. If the SQL returns any messages or data, psql displays them on the screen.<\/p>\n\n\n\n<p>To demonstrate how this works, create a text file named <strong>psql_script.sql<\/strong> and save it to a specific folder. For this example, I used my home folder to keep things simple when calling the file. Next, add the following <code>SELECT<\/code> statement to the file:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >SELECT col1, col2 FROM tbl \nWHERE col1 IN (102, 103, 104);\n<\/pre><\/div>\n\n\n\n<p>Once you\u2019ve set up your file, you can run the following psql command:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql -f psql_script.sql<\/pre><\/div>\n\n\n\n<p>The command takes the -f argument, followed by a value, which specifies the name of the target file. Because I saved the file to my home directory, I don\u2019t need to include the full path name, although you might need to include it in your command. When you run this statement, psql will return the query results to your command shell.<\/p>\n\n\n\n<p>You can also use psql to run SQL statements and meta-commands directly without entering interactive mode. In this case, you must use the -c option (instead of the -f option) and specify the statement or command in single quotes, as in the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql -c 'SELECT * FROM tbl WHERE col1 IN (105, 106);'<\/pre><\/div>\n\n\n\n<p>When you run this command, psql submits the <code>SELECT<\/code> statement to your ADP instance and returns the results to the command prompt, as shown in the following figure:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"454\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-5-1024x454.png\" alt=\"When you run this command, psql submits the SELECT statement to your ADP instance and returns the results to the command prompt, as shown in this image.\" class=\"wp-image-107524\" srcset=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-5-1024x454.png 1024w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-5-300x133.png 300w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-5-768x340.png 768w, https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2025\/08\/image-5.png 1090w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>You can also use this method to modify schema definitions. For instance, the following psql command runs a <code>DROP TABLE<\/code> statement that removes the tbl table from the <code>postgres<\/code> database:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql -c 'DROP TABLE tbl;'<\/pre><\/div>\n\n\n\n<p>This time, the statement returns only the message <code>DROP TABLE<\/code>. You can then verify that the table has been dropped by running the \\dt meta-command, as in the following example:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \" >psql -c '\\dt'<\/pre><\/div>\n\n\n\n<p>When you run this command, psql should return a message stating that it did not find any relations (tables), as expected. Being able to run psql commands non-interactively\u2014whether to run scripts, SQL statements, or meta-commands\u2014can be very useful when you need to perform quick one-offs or script your operations. The psql command also supports a number of other options for working with PostgreSQL without entering interactive mode.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Working with the psql command-line utility<\/h1>\n\n\n\n<p>The psql command-line utility is a versatile tool for working with PostgreSQL deployments, including ADP flexible server. You can run both SQL statements and meta-commands, choosing from a wide range of commands. You can also use psql interactively or non-interactively, as you\u2019ve just seen. The better you understand how psql works and the different operations you can perform, the more you can take advantage of what the tool offers.<\/p>\n\n\n\n<p>In this article, I touched upon a number of important aspects of psql to help you get started using the utility. However, there is a lot more to the tool than what I could cover in a single article. I\u2019ve already pointed you to the main <a href=\"https:\/\/www.postgresql.org\/docs\/current\/app-psql.html\" target=\"_blank\" rel=\"noreferrer noopener\">psql documentation<\/a>, which provides a comprehensive reference for using psql. I recommend that you spend some time going over this and learning about the different options. In the <a href=\"https:\/\/www.red-gate.com\/simple-talk\/databases\/postgresql\/using-pgadmin-to-work-with-azure-database-for-postgresql-flexible-server\/\" target=\"_blank\" rel=\"noreferrer noopener\">next article<\/a>, I introduce you to pgAdmin, a GUI management tool for PostgreSQL. It includes a built-in version of psql, giving you even more options for working with PostgreSQL and ADP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to install and use the psql command-line tool to connect to and manage Azure Database for PostgreSQL Flexible Server across macOS, Linux, and Windows.&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":105920,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143523,143534],"tags":[5364,4168,158978],"coauthors":[6779],"class_list":["post-107518","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-databases","category-postgresql","tag-azure","tag-database","tag-postgresql"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/107518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=107518"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/107518\/revisions"}],"predecessor-version":[{"id":107530,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/107518\/revisions\/107530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media\/105920"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=107518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=107518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=107518"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=107518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}