Running psql on windows

Getting Started with PostgreSQL on Windows

A Step by Step Guide to Installing and Setting up Postgres on your Windows Machine.

Jul 13, 2019 · 5 min read

So what is Postgres huh?😏

PostgreSQL, also known as Postgres, is a free and open-source relational database management system, emphasizing extensibility and technical standards compliance. It is designed to handle a range of workloads, from single machines to data warehouses or web services with many concurrent users. Basically, Postgres has superpowers; capable of handling very large datasets as though it was smashing peanuts.

Enough said, Who uses it?😒

Now, you are wondering who could possibly need all that. Well, thanks to its superpowers, a lot of firms use it. Including top-rank companies like Netflix, Uber, Spotify and etc.

Let’s get down to it!😎

I was on a p r oject, that I needed to scale, and part of the business requirement was Postgres. It was super frustrating to get it started on a Windows machine, due to lack of efficient tutorials and knowledge-base even the official site and documentation were not forthcoming.
To avoid the same stress I went through, and therefore make life relatively easier for you, follow the steps without missing/jumping any.

Step 1. Grab some Chocolatey 🍫:

Chocolatey is this badass software/package manager for Windows.
It lets you manage, install and uninstall packages like Postgres right from the Command Prompt (CMD)/PowerShell CLI.
To install Chocolatey, launch CMD as admin, run the following line, and let the magic begin.

When it’s done, CMD should return this line as one of the results:

We can also confirm that Chocolatey is now working by simply calling “choco” in CMD.
You should get the following result:

Step 2. Now stir in Postgres 🐘 with “choco”:

To grab and install Postgres;
Copy, paste and run the following line;
Note: Where it says ‘ passwordgoeshere ’, that’s the required password assigned to Postgres user, you can change it to a preferred password.

Now, this might take a while, depending on the on the speed of your PC and/or internet.
When it’s done, it might return some errors and failures, don’t be bothered.
To confirm that it has been installed;
This folder (“ C:\Program Files\PostgreSQL\10”) should have been created, containing all necessary installation files as seen below:

Step 3. Setting up the environment variables:

Now that we have Postgres on our machine, it would be nice to directly use Postgres in CMD by calling ‘ psql’ and/or ‘ pg_ctl’(just as we use Chocolatey by calling ‘ choco’).
To do this, you need to include Postgres in the system’s environment variables.
Windows+R should launch ‘Windows Run’ then you can swiftly type in ‘ SystemPropertiesAdvanced’ and continue, using the images and corresponding captions as seen below:

Читайте также:  Php ide and windows

Step 4. Kickstarting Postgres Database Cluster and Server:

After restarting your PC, navigate to the “ C:\Program Files\PostgreSQL\10\data” folder. Delete all files and sub-folders in that folder, so that it is completely empty.
In order to make use of this folder, you need to give ownership and grant full access to Everyone(User Group). To do this;
Launch CMD as admin, then run the following command:

We can now initialize the Postgres database cluster and server,
To do this; run the following command:

When it’s done initializing, CMD should return this line as one of the results:

Now that we have initialized, we can start the Postgres server,
To do this; run the following command:

Yuppie!,
It’s almost done.
Postgres server is now running.
Right out the box, Postgres creates three databases included ‘ postgres’.

Step 5. Interesting commands to get you started:

To list out the available databases, run:

To connect to a database in this case ‘postgres ’ since it’s available, run:

To disconnect from a database, run:

To create a database, run:

To create a user, run:

Here is a concise list of some interesting commands

  1. \? list all the commands
  2. \l list databases
  3. \conninfo display information about the current connection
  4. \c [DBNAME] connect to the new database, e.g., \c template1
  5. \dt list tables of the public schema
  6. \dt .* list tables of certain schema, e.g., \dt public.*
  7. \dt *.* list tables of all schemas
  8. Then you can run SQL statements, e.g., SELECT * FROM my_table; (Note: a statement must be terminated with a semicolon ; )
  9. \q quit psql

FINALLY! 🚀

You’re all set to use Postgres on your Machine.
Wondering why we didn’t do any GUI, or if a GUI exists for Postgres?
Well, yes a GUI exists, its called PGAdmin, the cool thing is that it comes as part of the installation we just did.

To use PGAdmin, just go to the Start menu and search ‘pgadmin’, you should click the program with an elephant🐘 icon.
Note: It’s generally adviced that all database query including the creation of the database itself, is done from an SQL file, this is so that changes can be tracked by ‘ git’ or any other version control packaged used.

WHAT NEXT? 🤔

Documentations are your friend, might not be expressly enticing but it gives you superpowers with the respective tool.
Postgres10 Official Doc: HERE

Set Up a PostgreSQL Database on Windows

To use Media Server with a PostgreSQL database, you must download and install a PostgreSQL server and ODBC driver, and configure Media Server to connect to the database through the driver.

The procedure describes setting up the database server using the psql command-line tool. If you prefer, you can use the pgAdmin graphical user interface. For more information, refer to the pgAdmin documentation on www.pgadmin.org.

To set up a PostgreSQL Media Server database on Windows

Download and install a PostgreSQL server. For instructions, refer to the PostgreSQL documentation on www.postgresql.org.

  • Ensure that the installation includes the PostgreSQL Unicode ODBC driver.
  • During installation, set up a user account with superuser privileges.
Читайте также:  Mac os yosemite совместимость

Once installed, the PostgreSQL server appears in the Services tab in Windows Task Manager.

Add the PostgreSQL bin directory path to the PATH environmental variable.

This step enables you to use the command psql to start the PostgreSQL command-line tool (psql) from the Windows Command Prompt. If the directory path is not added to the PATH variable, you must specify the psql.exe file path in the Command Prompt to start psql.

Open the psql command-line tool:

In the Windows Command Prompt, run the command:

Run a CREATE DATABASE command to create a new database. Specify the following database settings.

Database name Any name.
Encoding Must be Unicode–either UTF8 or UCS2.
Collation Any that is compatible with the encoding.
Locale Any that is compatible with the encoding.

Connect to the new database using the command:

Run the postgres.sql script provided in the Media Server installation directory. This script sets up the database schema that Media Server requires. The schema is inserted inside the public schema.

Micro Focus recommends running the following command to ensure that the script stops running if it encounters an error:

Run the script using the command:

where path is the script file path.

Replace backslashes in the file path with forward slashes. The psql command-line tool does not recognize backslashes in file paths.

Grant privileges to the user that Media Server will connect as. If security is not a consideration you could grant all privileges, but the required privileges are:

Database Create Temporary Tables
All tables Select, Insert, Update, Delete
All functions and stored procedures Execute
All sequences Usage
databaseName is the name of the database that you created.
userName is the user name that Media Server will connect as.

Open the Data Sources (ODBC) program:

In the Windows Control Panel, click System and Security.

The System and Security window opens.

Click Administrative Tools.

The Administrative Tools window opens.

Double-click Data Sources (ODBC).

The ODBC Data Source Administrator dialog box opens.

In the User DSN tab, click Add. .

The Create New Data Source dialog box opens.

Select the PostgreSQL Unicode driver from the list and click Finish.

The PostgreSQL Unicode ODBC Driver (psqlODBC) Setup dialog box opens.

Complete the data source information fields:

Whether to use SSL to connect to the database server.

To enable SSL mode, you must also configure the database server to support SSL. For instructions, refer to the PostgreSQL documentation.

Data Source The data source name (DSN). Media Server uses this string to connect to the database server.
Database The name of the database that you created in Step 2.
Server The IP address or hostname of the server that the database server is installed on.
User Name The user name to connect to the database server with.
Description An optional description for the data source.
SSL Mode
Port The port to use to communicate with the database server.
Password The password for the user account that connects to the database server.

The Advanced Options (driverName) 1/2 dialog box opens.

The Advanced Options (driverName) 2/2 dialog box opens.

Читайте также:  Vipnet client для линукс

Click Apply and then OK.

The Advanced Options (driverName) 2/2 dialog box closes.

In the PostgreSQL Unicode ODBC Driver (psqlODBC) Setup dialog box, click Test to test the connection.

The Connection Test box opens containing a message describing whether the connection was successful. If the connection failed, use the information in the message to resolve any issues.

Click OK to close the Connection Test box.

In the PostgreSQL Unicode ODBC Driver (psqlODBC) Setup dialog box, click Save to close the dialog box.

In the ODBC Data Source Administrator dialog box, click OK to close the dialog box.

  • You can now configure Media Server to connect to the database (see Configure Media Server).
  • Send documentation feedback

    To open the configured email client on this computer, open an email window.

    How do I install just the client tools for PostgreSQL on Windows?

    I have a PostgreSQL database on a Linux system that I want to access from my Windows PC. But the only Windows binaries I have been able to find are the full installer, which includes the database server and client.

    Is it possible to get a client-only Windows binary install for PostgreSQL from anywhere?

    (To clarify, I want the standard PostgreSQL client, psql — not a GUI client or independent tool).

    7 Answers 7

    Unfortunately there is no real client «only» installer.

    What you can do, is to download the ZIP archive of the complete Postgres binaries:

    and then remove the «server» part from it.

    When you unzip it, you get the following directories:

    You can remove the doc , include , pgAdmin III , StackBuilder and symbols directories. As far as I can tell (but I am not sure) the client also doesn’t need the share or lib directories, but you would need to test that. So that leaves only the bin directory.

    I think the share directory could be needed for localized error messages in psql but I’m not sure about that.

    Inside the bin directory you can essentially remove all .exe files (except psql.exe of course). You can also remove all wx*.dll files, they are only needed for pgAdmin. The libxml2.dll and libxslt.dll are also only needed for the server.

    If you do want some of the other client tools, you might want to keep

    One drawback of this approach is that this requires the Visual C++ Redistributable to be installed. But you can overcome that as well by simply putting the MSVCR120.DLL from some computer where it is installed into the bin directory.

    So that leaves you with these files (from the bin directory) that are required for the psql client:

    • iconv.dll (libiconv-2.dll in newer Postgres versions)
    • libeay32.dll
    • libintl-8.dll
    • libpq.dll
    • msvcr120.dll
    • ssleay32.dll
    • zlib1.dll
    • psql.exe

    Of course you can also take all that from an existing Postgres installation without the need to download the ZIP archive.

    It is obviously not a real installer, but if you put the cleaned up directory into a ZIP file, you can distribute that and whoever needs it just unzips the archive. Personally I find unzip to be the best «installer» anyway (I also use that to install the Postgres server, the Windows installer just has too many quirks)

    Оцените статью